Linux terminal commands are the key to unlocking the full potential of your Linux system. Whether you want to customize your desktop, automate tasks, troubleshoot issues, or perform advanced operations, Linux terminal commands can help you do it all.
But learning Linux terminal commands can be daunting for beginners. There are so many commands, options, and syntax rules to remember. How do you know which command to use, and how to use it correctly?
What are Linux Terminal Commands?
Linux terminal commands are instructions that you type in a terminal emulator, which is a program that allows you to interact with your Linux system using a text-based interface. A terminal emulator is also known as a shell, a console, or a command-line interface (CLI).
A terminal emulator can be launched from your graphical user interface (GUI), such as your desktop environment or your window manager, by clicking on an icon or pressing a keyboard shortcut. Alternatively, you can switch to a virtual console, which is a full-screen terminal that runs independently of your GUI, by pressing Ctrl+Alt+F1 to F6.
When you launch a terminal emulator, you will see a prompt, which is a symbol or a string of text that indicates that the terminal is ready to accept your commands. The prompt usually contains information such as your username, your hostname, your current directory, and a symbol such as $ or #. For example, a typical prompt may look like this:
user@host:~$
The symbol at the end of the prompt tells you whether you are logged in as a normal user ($) or as a superuser (#).
A superuser, also known as root, is a special user that has full access and control over the system. You can switch to the superuser mode by using the su or sudo commands, which will be explained later.
To execute a command, you simply type it after the prompt, and press Enter.
For example, to display the date and time, you can type:
user@host:~$ date
The terminal will then show you the output of the command, such as:
Sun Mar 3 15:57:04 PKT 2024
Some commands may require additional arguments, which are parameters that specify what the command should do.
For example, to list the files and folders in your current directory, you can type:
user@host:~$ ls
The terminal will then show you the output of the command, such as:
Desktop Documents Downloads Music Pictures Videos
But if you want to list the files and folders in a different directory, you need to provide the path of that directory as an argument to the ls command.
For example, to list the files and folders in your home directory, you can type:
user@host:~$ ls /home/user
The terminal will then show you the output of the command, which is the same as before, since your current directory is your home directory.
Some commands may also accept options, which are flags that modify the behavior of the command. Options usually start with a dash (-) or a double dash (–), and can be combined or separated.
For example, to list the files and folders in your current directory in a long format, with details such as size, permissions, owner, group, and modification date, you can use the -l option with the ls
command. You can also use the -a option to show hidden files and folders, which start with a dot (.). For example, to list all the files and folders in your current directory in a long format, you can type:
user@host:~$ ls -la
The terminal will then show you the output of the command, such as:
total 28 drwxr-xr-x 7 user user 4096 Mar 3 15:57 . drwxr-xr-x 3 root root 4096 Mar 3 15:57 .. -rw——- 1 user user 33 Mar 3 15:57 .bash_history -rw-r–r– 1 user user 220 Mar 3 15:57 .bash_logout -rw-r–r– 1 user user 3771 Mar 3 15:57 .bashrc drwxr-xr-x 2 user user 4096 Mar 3 15:57 Desktop drwxr-xr-x 2 user user 4096 Mar 3 15:57 Documents drwxr-xr-x 2 user user 4096 Mar 3 15:57 Downloads drwxr-xr-x 2 user user 4096 Mar 3 15:57 Music drwxr-xr-x 2 user user 4096 Mar 3 15:57 Pictures -rw-r–r– 1 user user 807 Mar 3 15:57 .profile drwxr-xr-x 2 user user 4096 Mar 3 15:57 Videos
As you can see, the output of the command is much more detailed and includes hidden files and folders.
To learn more about a command, its arguments, and its options, you can use the man command, which shows the manual page for the command. For example, to learn more about the ls command, you can type:
user@host:~$ man ls
The terminal will then show you the manual page for the ls command, which contains a description, a synopsis, a list of options, and some examples of usage. You can scroll up and down the manual page using the arrow keys, and quit by pressing Q.
The Most Useful and Common Linux Terminal Commands
Now that you know the basics of Linux terminal commands, let’s look at some of the most useful and common commands that you will need to master Linux like a pro.
Navigating Directories
One of the most essential skills in using Linux terminal commands is navigating directories, which are containers for files and folders. To navigate directories, you need to know two commands: pwd and cd.
The pwd command stands for print working directory, and it shows you the path of your current directory. For example, if you are in your home directory, the pwd command will show you something like this:
user@host:~$ pwd /home/user
The cd command stands for change directory, and it allows you to move to a different directory. To use the cd command, you need to provide the path of the directory that you want to move to as an argument.
For example, if you want to move to the Desktop directory, which is a subdirectory of your home directory, you can type:
user@host:~$ cd Desktop
The terminal will then change your current directory to the Desktop directory, and your prompt will reflect that change. For example, your prompt may look like this:
user@host:~/Desktop$
To move back to your home directory, you can use the cd command without any arguments, or with a tilde (~) as an argument, which is a shortcut for your home directory.
For example, you can type:
user@host:~/Desktop$ cd user@host:~$
or
user@host:~/Desktop$ cd ~ user@host:~$
To move to the parent directory of your current directory, which is the directory that contains your current directory, you can use the cd command with a dot dot (…) as an argument, which is a shortcut for the parent directory.
For example, if you are in the Desktop directory, and you want to move to the home directory, which is the parent directory of the Desktop directory, you can type:
user@host:~/Desktop$ cd .. user@host:~$
To move to the root directory, which is the top-level directory of your Linux system, you can use the cd command with a slash (/) as an argument, which is a shortcut for the root directory.
For example, you can type:
user@host:~$ cd / user@host:/$
host:~cd /usr/bin
user@host:/usr/bincd/usr/binuser@host:/usr/bin`
To move to a directory that is a subdirectory of your current directory, you can provide the relative path of the directory, which is the path that starts from your current directory and does not include the intermediate directories.
For example, if you are in the /usr directory, and you want to move to the /usr/bin directory, you can type:
user@host:/usr$ cd bin user@host:/usr/bin$
You can also use multiple arguments with the cd command to move to a directory that is several levels away from your current directory.
For example, if you are in the /usr/bin directory, and you want to move to the /home/user/Desktop directory, you can type:
user@host:/usr/bin$ cd ../../home/user/Desktop user@host:~/Desktop$
As you can see, the .. argument moves you up one level, and the / argument separates the directories in the path.
Copying, Moving, Renaming, and Deleting Files and Folders
cp
command
Another essential skill in using Linux terminal commands is copying, moving, renaming, and deleting files and folders. To do these operations, you need to know four commands: cp, mv, rm, and rmdir
.
The cp
command stands for copy, and it allows you to make a copy of a file or a folder. To use the cp
command, you need to provide the source and the destination as arguments. The source is the file or folder that you want to copy, and the destination is the file or folder that you want to create as a copy.
For example, if you want to make a copy of a file called report.txt
in your current directory, and name it report_copy.txt
, you can type:
user@host:~$ cp report.txt report_copy.txt
The terminal will then create a copy of the file report.txt
and name it report_copy.tx
t in your current directory.
If you want to make a copy of a file or a folder in a different directory, you need to provide the path of the destination as an argument. For example, if you want to make a copy of a file called report.txt
in your current directory, and place it in the Desktop directory, you can type:
user@host:~$ cp report.txt Desktop
The terminal will then create a copy of the file report.txt
and place it in the Desktop directory.
If you want to make a copy of a folder and its contents, you need to use the -r
option with the cp command, which stands for recursive. This option tells the cp
command to copy the folder and all the files and subfolders inside it.
For example, if you want to make a copy of a folder called project in your current directory, and name it project_copy, you can type:
user@host:~$ cp -r project project_copy
The terminal will then create a copy of the folder project and name it project_copy in your current directory, along with all the files and subfolders inside it.
mv
command
The mv
command stands for move, and it allows you to move a file or a folder from one location to another. To use the mv
command, you need to provide the source and the destination as arguments, just like the cp command. The source is the file or folder that you want to move, and the destination is the file or folder that you want to move it to.
For example, if you want to move a file called report.txt from your current directory to the Desktop directory, you can type:
user@host:~$ mv report.txt Desktop
The terminal will then move the file report.txt from your current directory to the Desktop directory.
The mv
command can also be used to rename a file or a folder, by providing a new name as the destination.
For example, if you want to rename a file called report.txt to report_final.txt in your current directory, you can type:
user@host:~$ mv report.txt report_final.txt
The terminal will then rename the file report.txt to report_final.txt in your current directory.
rm
command
The rm command stands for remove, and it allows you to delete a file or a folder. To use the rm command, you need to provide the name of the file or folder that you want to delete as an argument.
For example, if you want to delete a file called report.txt in your current directory, you can type:
user@host:~$ rm report.txt
The terminal will then delete the file report.txt from your current directory.
The rm command can also delete multiple files or folders at once, by providing more than one argument.
For example, if you want to delete two files called report.txt and report_copy.txt in your current directory, you can type:
user@host:~$ rm report.txt report_copy.txt
The terminal will then delete both files from your current directory.
The rm command can also delete all the files and folders in a directory, by using the -r
option, which stands for recursive, and the -f
option, which stands for force.
The -r option tells the rm command to delete the directory and all the files and subfolders inside it, and the -f option tells the rm command to delete the files and folders without asking for confirmation.
For example, if you want to delete a folder called project and all its contents in your current directory, you can type:
user@host:~$ rm -rf project
The terminal will then delete the folder project and all its contents from your current directory.
Warning: The rm command is very powerful and dangerous, as it can delete any file or folder permanently, without sending them to the trash or allowing you to recover them. Therefore, you should use the rm command with caution, and always double-check the arguments before executing it.
rmdir
command
The rmdir command stands for remove directory, and it allows you to delete an empty directory. To use the rmdir command, you need to provide the name of the directory that you want to delete as an argument.
For example, if you want to delete an empty directory called project in your current directory, you can type:
user@host:~$ rmdir project
The terminal will then delete the directory project from your current directory.
The rmdir command can only delete empty directories, meaning that they do not contain any files or subdirectories. If you try to delete a directory that is not empty, the rmdir command will show you an error message, such as:
user@host:~$ rmdir project rmdir: failed to remove ‘project’: Directory not empty
To delete a directory that is not empty, you need to use the rm command with the -r option, as explained before.
Searching for Files and Text
Another useful skill in using Linux terminal commands is searching for files and text. To search for files and text, you need to know two commands: find and grep.
The find command allows you to search for files and folders that match certain criteria, such as name, size, type, owner, group, permissions, and modification date.
To use the find command, you need to provide the path of the directory where you want to search, and the criteria that you want to match.
For example, if you want to search for all the files that have the extension .txt in your home directory, you can type:
user@host:~$ find ~ -name "*.txt"
The terminal will then show you the paths of all the files that have the extension .txt in your home directory, such as:
/home/user/report.txt /home/user/Desktop/notes.txt /home/user/Documents/resume.txt
The find command can also search for files and folders that have a certain size, by using the -size option, which accepts a number and a unit as an argument. The unit can be c for bytes, k for kilobytes, M for megabytes, or G for gigabytes.
For example, if you want to search for all the files that are larger than 10 megabytes in your home directory, you can type:
user@host:~$ find ~ -size +10M
The terminal will then show you the paths of all the files that are larger than 10 megabytes in your home directory, such as:
/home/user/Music/song.mp3 /home/user/Videos/movie.mp4 /home/user/Downloads/game.iso
The find command can also search for files and folders that have a certain type, by using the -type option, which accepts a letter as an argument. The letter can be f for regular files, d for directories, l for symbolic links, c for character devices, b for block devices, s for sockets, or p for pipes.
For example, if you want to search for all the directories in your home directory, you can type:
user@host:~$ find ~ -type d
The terminal will then show you the paths of all the directories in your home directory, such as:
/home/user /home/user/Desktop /home/user/Documents /home/user/Downloads /home/user/Music /home/user/Pictures /home/user/Videos
The find
command can also search for files and folders that have a certain owner, group, permissions, or modification date, by using the -user
, -group
, -perm
, or -mtime
options, respectively. These options accept different arguments depending on the criteria that you want to match.
For example, if you want to search for all the files that belong to the user user
in your home directory, you can type:
user@host:~$ find ~ -user user
The terminal will then show you the paths of all the files that belong to the user user
in your home directory, such as:
/home/user/report.txt /home/user/Desktop/notes.txt /home/user/Documents/resume.txt
To learn more about the find
command and its options, you can use the man
command, which shows the manual page for the command. For example, to learn more about the find
command, you can type:
user@host:~$ man find
The terminal will then show you the manual page for the find
command, which contains a description, a synopsis, a list of options, and some examples of usage. You can scroll up and down the manual page using the arrow keys, and quit by pressing Q.
grep
command
The grep
command allows you to search for text that matches a certain pattern, such as a word, a phrase, or a regular expression. To use the grep
command, you need to provide the pattern that you want to match, and the file or files that you want to search.
For example, if you want to search for the word Linux
in a file called report.txt
in your current directory, you can type:
user@host:~$ grep Linux report.txt
The terminal will then show you the lines of the file report.txt
that contain the word Linux
, such as:
Linux is a free and open source operating system. Linux is based on the Unix operating system. Linux is widely used for servers, desktops, and embedded devices.
The grep
command can also search for text in multiple files at once, by providing more than one file as an argument.
For example, if you want to search for the word Linux
in all the files that have the extension .txt
in your current directory, you can type:
user@host:~$ grep Linux *.txt
The terminal will then show you the lines of all the files that have the extension .txt
in your current directory that contain the word Linux
, along with the file names, such as:
report.txt:Linux is a free and open source operating system. report.txt:Linux is based on the Unix operating system. report.txt:Linux is widely used for servers, desktops, and embedded devices. notes.txt:Linux has many distributions, such as Ubuntu, Debian, and Fedora. notes.txt:Linux can be customized and modified by users and developers. notes.txt:Linux supports many programming languages, such as C, Python, and Java.
The grep
command can also search for text in the output of another command, by using a pipe (|), which is a symbol that redirects the output of one command to the input of another command.
For example, if you want to search for the word Linux
in the output of the ls
command, which lists the files and folders in your current directory, you can type:
user@host:~$ ls | grep Linux
The terminal will then show you the files and folders in your current directory that contain the word Linux
, such as:
Linux_Terminal_Commands_Guide.pdf Linux_Project.zip
The grep
command can also search for text that matches a regular expression, which is a sequence of characters that defines a complex pattern, such as a phone number, an email address, or a date. To use a regular expression with the grep
command, you need to use the -E
option, which stands for extended.
For example, if you want to search for all the phone numbers that have the format (xxx) xxx-xxxx
in a file called contacts.txt
in your current directory, you can type:
user@host:~$ grep -E "\([0-9]{3}\) [0-9]{3}-[0-9]{4}" contacts.txt
The terminal will then show you the lines of the file contacts.txt
that contain phone numbers that match the regular expression, such as:
Alice: (123) 456-7890 Bob: (234) 567-8901 Charlie: (345) 678-9012
To learn more about the grep
command and its options, you can use the man
command, which shows the manual page for the command.
For example, to learn more about the grep
command, you can type:
user@host:~$ man grep
The terminal will then show you the manual page for the grep
command, which contains a description, a synopsis, a list of options, and some examples of usage. You can scroll up and down the manual page using the arrow keys, and quit by pressing Q.
Compressing and Extracting Files and Folders
Another handy skill in using Linux terminal commands is compressing and extracting files and folders, which can help you save disk space, reduce file size, and transfer files faster. To compress and extract files and folders, you need to know two commands:
- tar
- gzip
tar
command
The tar command stands for tape archive, and it allows you to create or extract compressed archive files, which are files that contain one or more files or folders. To use the tar command, you need to provide the mode, the options, and the file name as arguments. The mode can be c for create, x for extract, or t for list.
The options can vary depending on the mode, but some common ones are v for verbose, f for file, and z for gzip. The file name is the name of the archive file that you want to create or extract.
For example, if you want to create a compressed archive file called project.tar.gz that contains the folder project and all its contents in your current directory, you can type:
user@host:~$ tar cvzf project.tar.gz project
The terminal will then create a compressed archive file called project.tar.gz in your current directory, and show you the files and folders that are being added to the archive, such as:
project/ project/report.txt project/code.py project/data.csv
The tar command can also extract a compressed archive file and restore the original files and folders. To do this, you need to use the x mode with the tar command, and provide the name of the archive file that you want to extract as an argument.
For example, if you want to extract the compressed archive file called project.tar.gz in your current directory, you can type:
user@host:~$ tar xvzf project.tar.gz
The terminal will then extract the compressed archive file project.tar.gz in your current directory, and show you the files and folders that are being restored, such as:
project/ project/report.txt project/code.py project/data.csv
The tar command can also list the contents of a compressed archive file without extracting it. To do this, you need to use the t mode with the tar command, and provide the name of the archive file that you want to list as an argument.
For example, if you want to list the contents of the compressed archive file called project.tar.gz in your current directory, you can type:
user@host:~$ tar tvzf project.tar.gz
The terminal will then list the contents of the compressed archive file project.tar.gz in your current directory, along with details such as size, permissions, owner, group, and modification date, such as:
drwxr-xr-x user/user 0 2024-03-03 15:57 project/ -rw-r–r– user/user 1024 2024-03-03 15:57 project/report.txt -rw-r–r– user/user 512 2024-03-03 15:57 project/code.py -rw-r–r– user/user 256 2024-03-03 15:57 project/data.csv
To learn more about the tar command and its options, you can use the man command, which shows the manual page for the command.
For example, to learn more about the tar command, you can type:
user@host:~$ man tar
The terminal will then show you the manual page for the tar command, which contains a description, a synopsis, a list of options, and some examples of usage. You can scroll up and down the manual page using the arrow keys, and quit by pressing Q.
gzip
command
The gzip command stands for GNU zip, and it allows you to compress or decompress individual files, using the gzip compression algorithm. To use the gzip command, you need to provide the name of the file that you want to compress or decompress as an argument.
For example, if you want to compress a file called report.txt in your current directory, you can type:
user@host:~$ gzip report.txt
The terminal will then compress the file report.txt in your current directory, and create a new file called report.txt.gz, which is the compressed version of the original file. The original file will be deleted, unless you use the -k option with the gzip command, which stands for keep.
For example, if you want to compress a file called report.txt in your current directory, and keep the original file, you can type:
user@host:~$ gzip -k report.txt
The terminal will then compress the file report.txt in your current directory, and create a new file called report.txt.gz, which is the compressed version of the original file. The original file will be kept, and you will have two files in your current directory: report.txt and report.txt.gz.
The gzip
command can also decompress a file that has been compressed with the gzip algorithm, and restore the original file. To do this, you need to use the -d option with the gzip command, which stands for decompress, and provide the name of the compressed file that you want to decompress as an argument.
For example, if you want to decompress a file called report.txt.gz in your current directory, you can type:
user@host:~$ gzip -d report.txt.gz
The terminal will then decompress the file report.txt.gz in your current directory, and create a new file called report.txt, which is the original version of the compressed file. The compressed file will be deleted, unless you use the -k option with the gzip command, which stands for keep.
For example, if you want to decompress a file called report.txt.gz in your current directory, and keep the compressed file, you can type:
user@host:~$ gzip -d -k report.txt.gz
The terminal will then decompress the file report.txt.gz in your current directory, and create a new file called report.txt, which is the original version of the compressed file. The compressed file will be kept, and you will have two files in your current directory: report.txt and report.txt.gz.
To learn more about the gzip command and its options, you can use the man command, which shows the manual page for the command. For example, to learn more about the gzip command, you can type:
user@host:~$ man gzip
The terminal will then show you the manual page for the gzip command, which contains a description, a synopsis, a list of options, and some examples of usage. You can scroll up and down the manual page using the arrow keys, and quit by pressing Q.