Linux is a powerful operating system that provides a wide range of command-line tools to manage the system. Linux commands are the primary way to interact with the operating system and perform tasks ranging from file management to system administration. In this article, we will discuss 30 Linux commands frequently used in Linux.
Basic Linux Commands
1. cd
The cd
command is used to change the current working directory. When you open a terminal, you are in the home directory by default. You can use the cd command followed by the directory path to move to a different directory. For example, to move to the /var/log
directory, you can use the command:
cd /var/log
2. ls
The ls
command lists the files and directories in the current working directory. By default, the ls
command displays only the file names. To display additional information, such as file size and permissions, you can use the -l
option. For example, to list the files and directories in the current directory with additional information, you can use the command:
ls -l
3. pwd
The pwd
command is used to print the current working directory. This command can be helpful when you need to know your current directory path.
4. mkdir
The mkdir
command is used to create a new directory. To create a directory named test
, you can use the command:
mkdir test
5. rmdir
The rmdir
command is used to remove an empty directory. To remove a directory named test
, you can use the command:
rmdir test
6. rm
The rm
command is used to remove files and directories. To remove a file named test.txt
, you can use the command rm test.txt
. To remove a directory and its contents, you can use the -r
option. For example, to remove a directory named test
and its contents, you can use the command:
rm -r test
7. cat
The cat
command is used to display the contents of a file. To display the contents of a file named test.txt
, you can use the command
cat test.txt
8. touch
The touch
command is used to create a new empty file. To create a file named test.txt
, you can use the command:
touch test.txt
9. echo
The echo
command is used to print text to the terminal. To print the text Hello World!
, you can use the command
echo "Hello World!"
Intermediate Linux Commands
10. grep
The grep
command is used to search for text within files. To search for the word test
in a file named test.txt
, you can use the command:
grep "test" test.txt
To search for text in multiple files, you can use the -r
option. For example, to search for the word test
in all files in the current directory and its subdirectories, you can use the command:
grep -r "test" .
11. find
The find
command is used to search for files and directories based on criteria such as name, size, and permissions. To find all files named test.txt
in the current directory and its subdirectories, you can use the command:
find . -name "test.txt"
To find all files larger than 1MB in the current directory and its subdirectories, you can use the command:
find . -size +1M
12. tar
The tar
command is used to create and extract archives. An archive is a file that contains other files and directories. To create a new archive named test.tar
containing the files in the current directory, you can use the command:
tar -cvf test.tar
To extract an archive named test.tar
, you can use the command:
tar -xvf test.tar
13. zip
The zip
command is used to compress and decompress files and directories. To create a new zip file named test.zip
containing the files in the current directory, you can use the command:
zip -r test.zip .
To extract a zip file named test.zip
, you can use the command:
unzip test.zip
14. scp
The scp
command securely copies files between remote and local systems. To copy a file named test.txt
from a remote system with IP address 192.168.1.1
to the local system, you can use the command:
scp user@192.168.1.1:/path/to/test.txt /local/path
15. ssh
The ssh
command is used to connect to remote systems over a secure network. To connect to a remote system with IP address 192.168.1.1
, you can use the command
ssh user@192.168.1.1
16. ps
The ps
command is used to display information about the running processes. By default, the ps
command displays the processes running in the current shell. To show all running processes, you can use the command:
ps -ef
17. top
The top
command is used to display real-time information about the system processes and resource usage. This command can be useful when you need to identify processes that are using too much CPU or memory.
top
18. ifconfig
The ifconfig
command is used to display network interface information. This command can be helpful when you need to know the IP address of a system or troubleshoot network issues.
19. netstat
The netstat
command is used to display information about network connections, routing tables, and network interface statistics. This command can be useful when you need to troubleshoot network issues or monitor network activity.
20. ping
The ping
command tests network connectivity by sending ICMP packets to a remote system. To ping a remote system with IP address 192.168.1.1
, you can use the command:
ping 192.168.1.1
21. dig
The dig
command is used to query DNS servers to retrieve information about domain names. To retrieve the IP address of the domain google.com
, you can use the command
dig google.com
22. curl
The curl
command transfers data to or from a server using different protocols such as HTTP, FTP, and SMTP. To download a file from a remote server, you can use the command.
curl -O <http://example.com/file.txt>
Advanced Linux Commands
23. awk
The awk
command is used to manipulate and process text files. This command can be used to extract data, transform data, and generate reports. To print the second column of a file named test.txt
, you can use the command
awk '{print $2}' test.txt
24. sed
The sed
command is used to perform text editing operations on files. This command can be used to search for patterns, replace text, and perform other text processing tasks. To replace the word **test
**with the word example
in a file named test.txt
, you can use the command
sed 's/test/example/g' test.txt
25. grep with Regular Expressions
The grep
command can be used with regular expressions to perform advanced text searching. Regular expressions are patterns that can match different types of text such as numbers, letters, and symbols. To search for all lines in a file named test.txt
that contain a number, you can use the command
grep '[0-9]' test.txt
26. find with Execution
The find
command can be used with the -exec
option to perform actions on the files or directories that match the search criteria. To delete all files named test.txt
in the current directory and its subdirectories, you can use the command
find . -name "test.txt" -exec rm {} \\;
27. tar with Compression
The tar
command can be used with compression options to create compressed archives. Compressed archives are smaller in size than regular archives and can be useful when transferring files over a network. To create a compressed archive named test.tar.gz
containing the files in the current directory, you can use the command
tar -czvf test.tar.gz .
28. ssh with Port Forwarding
The ssh
command can be used with port forwarding options to establish secure connections to remote systems and forward traffic to local ports. To forward traffic from port 8080 on the local system to port 80 on a remote system with IP address 192.168.1.1
, you can use the command
ssh -L 8080:localhost:80 user@192.168.1.1
29. iptables
The iptables
command is used to manage the firewall rules on Linux systems. This command can allow or block traffic to specific ports or IP addresses. To allow traffic to port 80 on a Linux system, you can use the command
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
30. crontab
The crontab
command is used to schedule and automate tasks on Linux systems. This command can run scripts or commands at specific times or intervals. To schedule a script named backup.sh
to run every day at 2 AM, you can use the command:
Enter crontab configuration file:
crontab -e
Write on the end of the file the following:
0 2 * * * /path/to/backup.sh
For more crontab configurations, we recommend checking crontab.guru.
Conclusion
In this article, we have discussed 30 Linux commands that are frequently used in Linux. These commands range from basic file management commands such as cd
, ls
, and mkdir
to advanced commands such as awk
, sed
, and iptables
. By mastering these commands, you can manage and administer Linux systems more efficiently.
📕 Related articles about Linux
- How to extract 7z file in Linux
- How to Enable Public Key Authentication in SSH
- How to Manage Failed Logins in SSH: Best Practices and Tips
- How to Remove a Directory in Linux
- How to check disk size and usage in Linux
- How to Fix “Remove Host Identification has Changed” Warning in SSH