Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3474

[HowTo] Command line basics for beginners

$
0
0
1. Why bother?
Because command line interface is the most powerful and efficient tool on your computer. It is such a waste not knowing even knowing the basics. Furthermore, GUIs can fail. Knowing the basics of the command line interface will make you able to troubleshoot and even fix issues that may happen under the hood, so to speak.

In this howto, we'll just go through the most basic commands for many differernt tasks, like navigating the system, manipulate files, manage processes, etc.

Learning just a few commands below will be extremely useful.

Terminal emulators are called Terminal (GNOME), Konsole (KDE Plasma), XFCE Terminal (XFCE), LXTerminal (LXDE), so launch the one that your deksktop environment uses.

Please note that you will need to navigate to the correct path to make most of these commands work. For instance, if I want to rename a file in /home/hallvor, I can just type mv document.odt howto.odt if I have already navigated to the directory with cd /home/hallvor. If I am outside that directory, I need to issue the full path to do the same, like this: mv /home/hallvor/document.odt /home/hallvor/howto.odt. Unless stated otherwise, I will assume that you have navigated to the correct path with the file(s) or folder(s) you want to alter.


2. Navigating the system


In this section we'll learn how to view lists of files in a directory and change directory


2.1 Viewing files


List (ls) the content of the current directory

Code:

ls

Make a detailed listing of the current directory

Code:

ls -l

Show hidden files

Code:

ls -a

Make a detailed list of the currect directory and list hidden files

Code:

ls -la

2.2 Change directory (cd)


E.g. navigate to /var/log

Code:

cd /var/log

Move up one directory level

Code:

cd ..

Navigate to the home directory

Code:

cd ~
Navigate to the previous directory

Code:

cd -


3. File management


In this section we'll learn how to copy, move, create and remove files and directories


3.1 Copy files and directories (cp)



Copy document1 to document2 (document2 is overwritten if already exists)

Code:

cp document1.odf document2.odf

Copy document1.odt from /home/hallvor to /home/hallvor/document

Code:

cp /home/hallvor/document1.odt /home/hallvor/document/document1.odt

Copy recursively (with all files) directory1 to directory2

Assuming we have the following directory structure with two directories in /home/hallvor, we can now copy all the content from dir1 to dir2. Please note that you must cd /home/hallvor and have made both directories for this to work:

Code:

/home/hallvor├── dir1│   ├── file1.txt│   ├── file2.txt│   └── subdir1│       └── file3.txt└── dir2

Code:

cp -r dir1 dir2          



3.2 Move/rename (mv) files and directories



Move (or rename) file1 to file2

Code:

mv file1 file2     



Move file1 to a directory

Code:

mv file1 /home/hallvor/

As above we can move the entire dir1 to dir2

Code:

/home/hallvor├── dir1│   ├── file1.txt│   ├── file2.txt│   └── subdir1│       └── file3.txt└── dir2

Code:

mv dir1 dir2


3.3 Remove (rm) files and directories


Warning: Please be careful executing rm commands, as they can make your system inoperable.

E.g. remove a file called file.txt

Code:

rm file.txt

Remove a directory and its content

Code:

rm -r directory       



Prompt before removng a file

Code:

rm -i file    




3.4 Creating files and directories



Create a new file called file.txt

Code:

touch file.txt

Create a new directory (mkdir) called documents

Code:

mkdir documents         




4. Viewing, editing and searching files



Display the content of a file, e.g.

Code:

cat file.txt                       
View contents page by page

Code:

less file.txt

Open the file in nano text editor

Code:

nano file.txt

Search for files based on patterns

Code:

grep 'pattern' file.txt

Recursively search for pattern in a directory, for instance

Code:

grep -r 'pattern' /home/hallvor


5. Process management


Use these commands to show and terminate processes on your computer


Show current user processes

Code:

ps

Show all processes

Code:

ps aux

Interactive process viewer

Code:

top

Terminate a process with a given PID (number is listed in the second column in the "top" command), e.g.

Code:

kill 29525

Terminate a process with force, e.g.

Code:

kill -9 29525


6. Networking

Test network connecticity by pinging a remote host three times, e.g.

Code:

ping -c3 google.com

The following comands are essential when it comes to troubleshooting networking issues:

Show IP addresses

Code:

ip addr show

Show all network interfaces on your system

Code:

ip link show

Display the routing table

Code:

ip route show

7. File permissions and ownership



Set read/write for the owner, read permissions for others

Code:

chmod 644 file   


Add execute permissions to file

Code:

chmod +x file 



Change file owner and group

Code:

chown user:group file     




8. Archiving and Compression


Create a tar archive from a directory

Code:

tar -cvf archive.tar directory

Extract a tar archive

Code:

tar -xvf archive.tar   



Compress a file

Code:

gzip file 



Decompress a file

Code:

gzip -d file.gz

9. Services


A service is a program running in the background while performing certain tasks for your system. # means that the command must be run as root or with sudo. Please replace "service-name" below with the real name of your service.


List all services

Code:

systemctl list-units --type=service

Start a service

Code:

# systemctl start service-name

Stop a service

Code:

# systemctl stop service-name
Restart a service

Code:

# systemctl restart service-name

Reload a service

Code:

# systemctl reload service-name

Enable a service:

Code:

# systemctl enable service-name

Disable a service

Code:

# systemctl disable service-name

Check the status of a service

Code:

systemctl status service-name

Mask a service (prevent a service from being started)

Code:

# systemctl mask service-name

Unmask a service

Code:

# systemctl unmask service-name

View logs for a service

Code:

# journalctl -u service-name


10. Logs



View the logs since last boot

Code:

journalctl -b

Filter logs by a specific service, for instance

Code:

journalctl -u apache2

View live logs

Code:

journalctl -f

List errors only

Code:

journalctl -p err

Display log statistics, for instance size

Code:

journalctl --disk-usage 

Clear all logs (as root or sudo)

Code:

# journalctl --rotate# journalctl --vacuum-time=1s

That's it! I hope you have found it useful. Please note that these are just the most basic commnds and parameters, and that you can find more info in the manpages. If you for instance want more parameters for the cp command, just issue

Code:

man cp
in your command line interface emulator.

Statistics: Posted by Hallvor — 2024-07-17 14:24 — Replies 2 — Views 76



Viewing all articles
Browse latest Browse all 3474

Trending Articles