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
Make a detailed listing of the current directory
Show hidden files
Make a detailed list of the currect directory and list hidden files
2.2 Change directory (cd)
E.g. navigate to /var/log
Move up one directory level
Navigate to the home directory
Navigate to the previous directory
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)
Copy document1.odt from /home/hallvor to /home/hallvor/document
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:
3.2 Move/rename (mv) files and directories
Move (or rename) file1 to file2
Move file1 to a directory
As above we can move the entire dir1 to 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
Remove a directory and its content
Prompt before removng a file
3.4 Creating files and directories
Create a new file called file.txt
Create a new directory (mkdir) called documents
4. Viewing, editing and searching files
Display the content of a file, e.g.View contents page by page
Open the file in nano text editor
Search for files based on patterns
Recursively search for pattern in a directory, for instance
5. Process management
Use these commands to show and terminate processes on your computer
Show current user processes
Show all processes
Interactive process viewer
Terminate a process with a given PID (number is listed in the second column in the "top" command), e.g.
Terminate a process with force, e.g.
6. Networking
Test network connecticity by pinging a remote host three times, e.g.
The following comands are essential when it comes to troubleshooting networking issues:
Show IP addresses
Show all network interfaces on your system
Display the routing table
7. File permissions and ownership
Set read/write for the owner, read permissions for others
Add execute permissions to file
Change file owner and group
8. Archiving and Compression
Create a tar archive from a directory
Extract a tar archive
Compress a file
Decompress a file
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
Start a service
Stop a service
Restart a service
Reload a service
Enable a service:
Disable a service
Check the status of a service
Mask a service (prevent a service from being started)
Unmask a service
View logs for a service
10. Logs
View the logs since last boot
Filter logs by a specific service, for instance
View live logs
List errors only
Display log statistics, for instance size
Clear all logs (as root or sudo)
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
in your command line interface emulator.
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 ~
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
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
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
Statistics: Posted by Hallvor — 2024-07-17 14:24 — Replies 2 — Views 76