This is an English translation of the provided Chinese text. Please find the translation below:
Linux Commands to Master
Category Programming Technology
Click on the image to view it in large size
1. tar
Create a new tar file
$ tar cvf archive_name.tar dirname/
Unpack a tar file
$ tar xvf archive_name.tar
View the contents of a tar file
$ tar tvf archive_name.tar
2. grep
Search for a string in a file (case insensitive)
$ grep -i "the" demo_file
Output the matching line and the following three lines
$ grep -A 3 -i "example" demo_text
Recursively search for files containing a specified string in a folder
$ grep -r "ramesh" *
3. find
Find files with a specified name (case insensitive)
$ find -iname "MyProgram.c"
Execute a command on found files
$ find -iname "MyProgram.c" -exec md5sum {} \;
Find all empty files in the home directory
$ find ~ -empty
4. ssh
Log in to a remote host
$ ssh -l jsmith remotehost.example.com
Debug the ssh client
$ ssh -v -l jsmith remotehost.example.com
Display the version of the ssh client
$ ssh -V
5. sed
When you copy a file from a Dos system to Unix/Linux, the file will end with \r\n on each line. Sed can easily convert it to a Unix format file, using \n as the file ending
$ sed 's/.$//' filename
Reverse the content of a file and output
$ sed -n '1!G; h; p' filename
Add line numbers to non-empty lines
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'
6. awk
Delete duplicate lines
$ awk '!($0 in array) { array[$0]; print}' temp
Print all lines in /etc/passwd that contain the same uid and gid
$ awk -F ':' '$3=$4' /etc/passwd
Print specific fields of a file
$ awk '{print $2,$5;}' employee.txt
7. vim
Open a file and jump to the 10th line
$ vim +10 filename.txt
Open a file and jump to the first matching line
$ vim +/search-term filename.txt
Open a file in read-only mode
$ vim -R /etc/passwd
8. diff
Compare while ignoring whitespace
$ diff -w name_list.txt name_list_new.txt
9. sort
Sort the contents of a file in ascending order
$ sort names.txt
Sort the contents of a file in descending order
$ sort -r names.txt
Sort the contents of /etc/passwd by the third field
$ sort -t: -k 3n /etc/passwd | more
10. export
Output environment variables that match the string oracle
$ export | grep ORCALE
declare -x ORACLE_BASE="/u01/app/oracle"
declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
declare -x ORACLE_SID="med"
declare -x ORACLE_TERM="xterm"
Set a global environment variable
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0
11. xargs
Copy all image files to an external drive
$ ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
Compress all jpg files in the system into a package
$ find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
Download all pages corresponding to the urls listed in the file
$ cat url-list.txt | xargs wget –c
e: USER = User Name z: Flags = Task Flags
If you only want to display processes for a specific user, you can use the -u option
$ top -u oracle
25. df
Displays disk usage for file systems. By default, df -k will output disk usage in bytes.
$ df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 29530400 3233104 24797232 12% /
/dev/sda2 120367992 50171596 64082060 44% /home
Using the -h option will display disk usage in a more readable format.
$ df -h
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk0s2 232Gi 84Gi 148Gi 37% 21998562 38864868 36% /
devfs 187Ki 187Ki 0Bi 100% 648 0 100% /dev
map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /home
/dev/disk0s4 466Gi 45Gi 421Gi 10% 112774 440997174 0% /Volumes/BOOTCAMP
//[email protected]/public 2.7Ti 1.3Ti 1.4Ti 48% 0 18446744073709551615 0% /Volumes/public
Use the -T option to display the file system type.
$ df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda1 ext4 29530400 3233120 24797216 12% /
/dev/sda2 ext4 120367992 50171596 64082060 44% /home
26. kill
The kill command is used to terminate a process. Generally, we first use ps -ef to find a process and get its process ID, then use kill -9 process ID to terminate the process. You can also use killall, pkill, xkill to terminate processes.
$ ps -ef | grep vim
ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim
$ kill -9 7243
27. rm
Confirm before deleting a file.
$ rm -i filename.txt
Using shell wildcards in the file name can be very useful. Print the file name and confirm before deleting.
$ rm -i file*
Recursively delete all files in the folder and then delete the folder itself.
$ rm -r example
28. cp
Copy file1 to file2, and preserve the file's permissions, owner, and timestamps.
$ cp -p file1 file2
Copy file1 to file2, if file2 exists, prompt whether to overwrite.
$ cp -i file1 file2
29. mv
Rename file1 to file2, if file2 exists, prompt whether to overwrite.
$ mv -i file1 file2
Note that if you use the -f option, no prompt will be given.
-v will output the renaming process, which is very convenient when the file name contains wildcards.
$ mv -v file1 file2
30. cat
You can view the contents of multiple files at once. The following command will first print the contents of file1, then print the contents of file2.
$ cat file1 file2
The -n command adds line numbers in front of each line.
$ cat -n /etc/logrotate.conf
/var/log/btmp {
missingok
3 monthly
4 create 0660 root utmp
5 rotate 1
6 }
31. mount
If you want to mount a file system, you need to first create a directory and then mount the file system to this directory.
# mkdir /u01
# mount /dev/sdb1 /u01
You can also add it to fstab for automatic mounting, Log in as a specified user and use a specified shell program instead of the default one.
$ su -s 'SHELLNAME' USERNAME
45. mysql
MySQL may be the most widely used database on Linux. Even if you haven't installed MySQL on your server, you can use the MySQL client to connect to a remote MySQL server.
To connect to a remote database, you need to enter a password.
$ mysql -u root -p -h 192.168.1.2
To connect to a local database.
$ mysql -u root -p
You can also enter the database password in the command line, just add the password as an argument after -p, you can write it directly after 'p' without a space.
46. yum
Use yum to install apache.
$ yum install httpd
Update apache.
$ yum update httpd
Uninstall/Remove apache.
$ yum remove httpd
47. rpm
Use rpm to install apache.
# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm
Update apache.
# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm
Uninstall/Remove apache.
# rpm -ev httpd
48. ping
Ping a remote host, sending only 5 packets.
$ ping -c 5 gmail.com
49. date
Set the system date.
# date -s "01/31/2010 23:59:53"
When you change the system time, you need to synchronize the hardware time with the system time.
# hwclock –systohc
# hwclock --systohc –utc
50. wget
Use wget to download software, music, and videos from the internet.
$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
Download a file and save it with a specified file name.
$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701
>
Translation: https://zhuanlan.zhihu.com/p/28674639
Original text: http://www.thegeekstuff.com/2010/11/50-linux-commands/