25 Best Linux Commands All Users Should Learn
Linux by providing a Command-Line Interface (CLI), allows users to interact with the Linux system more efficiently through running commands.
Learning the basic Linux commands is essential for more effective interaction with the Linux system and managing and performing a wide range of tasks more efficiently and quickly.
Top 25 essential Linux command you must Know
Command | Function |
---|---|
ls | Lists current directory’s contents. |
cd | Changes the current directory. |
pwd | Displays the current directory path. |
mkdir | Creates new directories. |
rm | Removes files or directories. |
cp | Copies files or directories to another location. |
mv | Moves files or directories to another location. |
grep | Searches for text within files. |
touch | Creates an empty files |
head | Displays the beginning of file. |
tail | Displays end of file. |
rmdir | Deletes empty directories. |
file | Provides information about file type. |
sort | Reorders (ascending or descending order) content of text file. |
tar | Archives and compression multiple files into a single file. |
awk | Searchs, extracts and manipulates data from text files |
df | Displays disk space usage. |
chmod | Change file permissions. |
chown | Modifies file ownership. |
sed | Finds and transforms text in a file or from input. |
du | Shows disk usage of files and directories. |
cat | Prints file contents. |
echo | Displays a line of text that follows the command. |
locate | Immedietly finds file by name. |
wget | Downloads files or app directly from the web. |
Kill | Terminate the current process using process ID. |
killall | Terminate the current process using process name. |
ps | Displays details about the current running process. |
Mastering basic Linux commands after purchasing a Linux VPS is essential to benefit from the potential power of a Linux server and managing it efficiently.
Therefore, connect to your Linux server, which is running a modern Linux distribution, via SSH using PuTTY or Terminal, and with root access, utilize the capabilities of these handy command-line tools.
1. The ls command:
The ls
command (short for List) in Linux is for listing the contents of the current directories and files. The ls command in Linux is a useful tool for displaying information about files and directories within the specified directory.
The ls
command is going to be one of the most used Linux commands; the basic syntax of the ls
command in Linux is as follows:
ls
This command without any arguments, lists the files and directories in the current directory.
To list specific directories, you can run the following command:
ls /path/to/directory
The ls command supports additional options to offer flexibility. Here are some of the common options for ls
command:
- -l: shows detailed information including file permissions, owner, size, and date.
- -lh: lists long format with size conversion to readable formats (e.g., K, M, G).
- -a: shows hidden files
- -R: lists all files and directories recursively.
2. The cd command:
The cd
(change directory) command in Linux allows you to navigate through the directories in the file system and change the current directory while working within the terminal. To do this, run the following syntax with sudo privileges:
cd <directory path>
If you want to change to the Home directory, you can use the following commands:
cd
Or
cd ~
The cd
command like all of the Linux commands let you customize its behavior with its options, including:
- cd ..: Moves up one directory level.
- cd –: Changes to the previous directory.
- cd ~[username]: Switch to another user’s home directory.
- cd subdirectory_name: Navigates to the subdirectory In the current working directory.
3. The pwd command:
The pwd
(print working directory) command in Linux is used for displaying the full path of the current working directory like /home/directory/path. Its general syntax is:
pwd [options]
The pwd
command also supports options such as:
- -L: Prints current working directory logical path with symbolic links.
- -P: Prints the current directory’s actual path without the symbolic links.
4. The mkdir command:
If you wish to create new directories in the terminal, the mkdir
(make directory) command is the solution. In addition, the mkdir command in Linux, lets you set permissions and create multiple folders simultaneously. The syntax of this command for creating a single directory is as follows:
mkdir [option] [directory_name]
To create multiple directories at once, you can use the following command:
mkdir [directory1, directory2, directory3 ]
You can also specify a path when creating a new directory. for example to create a directory named new/directory within the path/to/, run the following command:
mkdir /path/to/new/directory
To create a directory with specific permissions, use the -m option along with mkdir
command. For example to create a directory with read, write, and execute permissions for all users, run the following command:
mkdir -m777 directory_name
5. The rm command
The rm
(remove) command is one of the important Linux commands for deleting files or directories. However, when using rm
command, you should have the necessary permissions to delete the specified files or directories and be cautious.
The rm
command in Linux deletes files permanently and there is no undo feature in Linux, so use rm
command carefully.
The syntax to remove the specified file or directory is as follows:
rm file_name
Here are some common options for the rm
command:
- -r: Deletes a directory and its contents, including subdirectories and files.
- -i: Prompts for confirmation before deleting each file.
- -f: Forces the deletion of the file without prompting for confirmation.
6. The cp command
The cp
(copy) command is a handy tool in Linux for managing your file system. The cp command in Linux is used for copying files and directories from one location to another. Its syntax is as follows:
cp [source_file] [destination_file]
source_file: the file to want to create a copy.
destination_file: the location of the copied file.
For example:
cp image.jpg ~/Downloads
This command copies “image.jpg” to the Downloads directory.
The source file and destination file can not be the same.
If the destination_file does not exist, the cp
command will create the specified file, and if this file already exists, its content will be overwritten.
You can also copy multiple files to a directory by using the following syntax:
cp file1.txt file2.txt /destination_directory/
If you want to set a prompt for confirmation before overwriting any existing files in the destination, you can use the “-i” option with the cp
command in Linux:
cp -i file1.txt /destination_directory/
7. The mv command
The mv
command in Linux is a useful tool for moving and renaming files and directories. Its basic syntax for moving files in Linux is as follows:
mv [source] [destination]
For example, to move “report.txt” to the “documents” directory, run the following command:
mv report.txt documents/
For instance, to rename a “report.txt” to a “report_final.txt” file, use the following command:
mv report.txt report_final.txt
To move a directory “dir1” to another location “dir2“, run the following command:
mv dir1 dir2
The mv
command lets you customize its behavior by offering several options:
- -i: Enables interactive mode and prompts for confirmation before overwriting.
- -f: Forces to move a file and overwrite the destination without prompting. (use it carefully.)
- -v: enables verbose mode and explains what is being done when moving files.
8. The grep command
The grep
(global regular expression print) command which is a versatile search tool in Linux, enables searching text patterns in files. To use the grep command in Linux for searching and filtering text in files and command output, run the following command:
grep [options] pattern [file...]
pattern: The string or regular expressions to search for.
file: one or more files to search in.
For example, to search for the string “example” in a file called file.txt, run the following command:
grep "example" file.txt
This command prints all lines containing the strings (example).
The grep
command is a powerful and valuable tool for filtering large log files.
Numerous options are available with grep
, here are some ones:
- -i: Enables case-insensitive manner in pattern search.
- -c: Counts the number of matching lines.
- -r or -R: Searches for patterns in all files in the current directory and subdirectories.
- -v: Inverts the search, and displays lines that do not match the pattern.
- -l: lists filenames that contain patterns.
9. The touch command
The touch
command in Linux is a versatile tool for managing file creation and modification times.
The touch command in Linux lets you create new empty files or update the timestamp of existing files. Here is its syntax:
touch [options] [file name]
This command creates a new empty file if it does not exist or modify the last access and modification times of existing files.
The touch
command like other Linux commands offers various flags for specific needs, including:
- -a: Updates only the access time.
- -m: Updates only the modification time.
10. The head and tail commands
The head
and tail
commands in Linux display the beginning and end parts of the file. These commands enable efficiently view and monitor specific parts of files, which is useful for monitoring log files or large files.
The head
command is used for the first part of the file (first 10 Lines by default). Its basic syntax is as follows:
head [options] [file...]
You can specify the number of lines to display using the following command:
head -n num filename.txt
The tail command in Linux is a handy tool for displaying the last part of the file (the last 10 lines by default). Its basic syntax is as follows:
tail [options] [file...]
This command by default prints the last 10 lines of the specified file.
You can specify the number of lines to show using the following command:
tail -n num filename.txt
For example to show the last 20 lines of “file.txt” run the following command:
tail -n 20 file.txt
To display the last 10 lines of multiple files, use the following command:
tail file1.txt file2.txt
11. The rmdir command
Use the rmdir
command in Linux to remove empty directories. The rmdir
command does not delete directories that contain files or other directories, it only deletes empty directories. The rmdir
command basic syntax in Linux is as follows:
rmdir [options] [directory name]
To run the rmdir
command you must have sudo
privileges.
If this command prints an error message, it means the folder might contain a subdirectory. To remove a directory and its contents, you can use the rm
command with the “-r” option. On the other hand, you can use the ” –ignore-fail-on-non-empty” option with rmdir
command to force delete non-empty directories and ignore failures.
12. The file command
If you want to get information about a file, the file
command is the best solution. By running a file command in Linux, you can determine the type of a file and its true nature. To use the command, run:
file [filename]
This command prints the type of a file (e.g., ASCII text file, PDF document), regardless of its extension. Using file
command you can understand a file’s content before opening it.
13. The sort command
The sort
command is a valuable command-line tool for organizing and managing text files.
The sort
command is used to arrange lines of text files in a specific order based on different criteria. By default, it sorts the lines alphabetically or numerically (in ascending order). The basic syntax of the sort
command is:
sort [filename]
You can customize sorting order with different options, including:
- -r: Sorts the lines in descending order.
- -n: Sorts the lines in numerical order.
- -k field_number: Sorts based on a particular field (column) within each line.
- -u: Sorts lines and removes any duplicate lines.
14. The tar command
The tar
command in Linux is a widely used tool for archiving and compressing files. The tar
command in Linux can be introduced as an archive manager which allows you to create, extract, and manage archive files.
The tar
command is often used for combining multiple files into a single archive file (often called a tarball). Therefore, it results in reducing file size using compression.
The basic syntax of the tar
command is:
tar [options] [archive_name] [file_names or directory]
For example, to create a new “archive_name.tar” in the “/home/user/Documents” directory, run the following command:
tar -cvzf archive_name.tar /home/user/Documents
To extract an archive ” archive_name.tar “, run the following command:
tar -xvf archive_name.tar
15. The awk command
The awk
command in Linux is a versatile tool for data extraction and processing text. It lets you extract specific lines based on specified patterns, manipulate data, and create reports based on the processed data. Here is the basic syntax:
awk 'pattern {action}' filename
pattern: Defines which lines to process. (specifies action function)
action: What to do when the pattern matches.
For instance, to print lines from “data.txt” where the first field (denoted by $1) is greater than 10, run:
awk '$1 > 10 {print $0}' data.txt
16. The df command
The df
(disk filesystem) command is a very efficient tool for monitoring disk space usage on your filesystems when working with a Linux system. It displays information about the available disk space and the usage of the mounted filesystems. Here is the basic syntax:
df [options] [file]
To display information only for the specific directory for example the /home file system, use the following command:
df /home
Numerous options are available with the df
command, including:
- -h: Displays disk space usage in a human-readable format (e.g., KB, MB, GB).
- -T: Shows filesystem type.
17. The chmod and chown commands
By mastering chmod
and chown
commands, you can effectively manage file permissions and ownership.
The chmod command in Linux is a useful tool for modifying the access permissions (read, write, and execute permissions) of a file or directory. The default syntax for chmod
command is as follows:
chmod [options] [permission] [filename]
Permissions are set by a combination of three attributes: r(Read), w (Write), and x (Execute).
Permissions can be applied for three types of users: owner (u), group (g), and others (o).
You can also use the Numeric (Octal) value for permissions:
- Read=4
- Write=2
- Execute=1
To set desired permissions, you can sum these values, for example, to set rwx (read, write, and execute permissions), you must add ‘7‘ to the chmod
command.
For instance, to set read, write, and execute permissions for the owner, and read and execute permissions for the group and others using numeric representation, run the following command:
chmod 755 file.txt
To set owner read, write, and execute permissions, and others only read permission using symbolic representation, use the following command:
chmod u=rwx,go=r file.txt
The “=” symbol is used for setting exact permission. You can also use “– (remove permission)” and “+ (add permission)” with chmod
command in Linux.
The chown command in Linux is used for changing ownership of a file or directory. Its default syntax is as follows:
chown [options] user [:group] file
To change the file ownership, replace the user with a username or user ID and group with a group name or group ID (optional) in the command syntax.
For example, change the owner of a file to “Opera” using the following command:
chown Opera file.txt
Note: you must have administrative privileges ( sudo
) for running these commands.
18. The sed command
The sed
(stream editor) command with its high level of flexibility, lets you manipulate text and automate many editing tasks. It searches, finds & replaces, inserts/deletes lines, and modifies the text of files without opening them in an editor. Here is the basic syntax to edit file using sed command in Linux:
sed 's/old_text/new_text/' filename.txt
old_text: pattern to find.
new_text: replacement text.
filename.txt: file to edit.
19. The du command
The du
(disk usage) command is another valuable tool for managing and monitoring disk space usage on the Linux system. It is useful for identifying which files are consuming the most space.
To display the amount of disk space used by files or directories, run the following command:
du [options] [path/to/directory or file]
There are various options with du command to customize the output to suit your needs, including:
- -h: Displays disk usage in human-readable format.
- -a: Shows disk usage for all files.
- -s: Prints only the total size for each argument.
- -sh: Summarize disk usage by displaying only the total size of each directory.
20. The cat command
The cat
(concatenate) command in Linux displays the contents of a file on the terminal. Learning how to use the cat command in Linux is beneficial for working with files and helps to view file contents, combine multiple files, and create new files effectively. Here is its basic syntax:
cat filename
You can combine multiple files into a new one using cat
command:
cat file1 file2 > newfile
21. The echo command
The echo command in Linux prints line of text and variables in the terminal or redirects output to files. The basic syntax of the echo
command is:
echo [options] [string]
For example, to display a simple string on the terminal, use the echo
command:
echo "Hello, World!"
you can print the value of a variable with echo
command, for example:
echo "Hello, $name!"
The echo
command is a powerful tool for redirecting output to a file (overwriting the file):
echo "Hello, World!" > file.txt
Also, you can append text to an existing file using echo
command:
echo "Hello, again!" >> file.txt
22. The locate command
The locate
command in Linux is a fast and efficient way to find files by their names. The basic syntax of locate
command in Linux is:
locate [filename]
This command searches a pre-built database of filenames and their paths to locate files and directories quickly.
For example, to find all files named “config“, run the following command:
sudo locate config
The locate
command also supports various options, such as:
- -i: Searches for a file name case-insensitively.
- -c: Counts the number of matching entries.
- -r: Uses expression to search for files.
23. The wget command
The wget
command in Linux lets you download files from the web.
Use the following syntax to download a file:
wget [options] [URL]
The wget command in Linux retrieves files using HTTP, HTTPS, and FTP protocols and it works in the background even if you are not logged in.
Downloading multiple files at once is one of its valuable features.
Here are some common options for the wget command:
- -O filename: Saves downloaded file with a specific name.
- -c: Resumes a partially previous download.
- -q: Activates Quiet mode and suppresses output messages.
- -b: Activates background mode and enables running in the background.
24. The Kill and killall commands
The kill
and killall
commands in Linux are used for terminating processes based on their Process ID (PID) and name. These two commands are very efficient for optimal management of processes if you know the name or PID of the process.
The kill command stops the processes by Process ID (PID). Its basic syntax is as follows:
kill [signal] [PID]
signal: There are various signals such as SIGTERM(15), SIGKILL(9), and SIGHUP(1) which are sent to processes.
PID: PID of the processes to terminate.
For example, if you want to stop the process with PID 1234 gracefully (default signal), run the following command:
kill 1234
If you want to forcefully terminate a process with PID 1234, use the following command:
kill -9 1234
The 9 value is for the SIGKILL signal which forcefully kills the process.
If you do not know the PID of the process, you can find the PID for every process using the ps
command.
Another method to kill the process without needing PID is using killall
command. The killall
command lets you terminate the process with the name. Here is its syntax:
killall [SIGNAL] [process_name]
For example, to kill all Firefox processes, run the following command:
killall firefox
Note: To get more details about signals, refer to “man 7 signal
“.
25. The ps command
The ps
(process status) command in Linux provides a snapshot of the current processes with details such as PID (Process ID), TTY (terminal type), CPU usage, memory usage, and command name.
Its basic syntax to display information about the currently running processes is:
ps [options]
The ps command in Linux offers a vast array of options to show specific details based on various criteria, including:
- -ef: Displays a detailed listing of all processes.
- -e: shows all processes on the system.
- -u username: Filters processes by user.
Conclusion
Command-line tools are powerful for greater control over system security and configurations, diagnosing and solving system issues, and enhancing the overall productivity of Linux systems.
Mastering basic Linux commands equips individuals with valuable skills that allow for efficient Linux system management, monitoring, and troubleshooting of Linux systems.
To learn more about the Linux commands and their usage, you can refer to the shell’s manual using ” man [command]
” or check the shell’s help system using ” help [command]
“.
Learning basic Linux commands is essential for making the most out of your Linux VPS.