50 most commonly used Unix/Linux commands

This article translated from www.thegeekstuff.com/2010/11/50-… These are some of the most common commands, and each of the commands in this article has a few simple examples of how it can be used, which are basically required for anyone who wants to learn Unix/Linux:

tar grep find ssh sed awk vim diff sort export
args ls pwd cd gzip bzip2 unzip shutdown ftp crontab
service ps free top df kill rm cp mv cat
mount chmod chown passwd mkdir ifconfig uname whereis whatis locate
man tail less su mysql yum rpm ping date wget

1. tar

Create a new tar file

$ tar cvf archive_name.tar dirname/
Copy the code

Decompress the tar file

$ tar xvf archive_name.tar
Copy the code

Viewing the tar file

$ tar tvf archive_name.tar
Copy the code

2. grep

Finding a string in a file (case insensitive)

$ grep -i "the" demo_file
Copy the code

Prints the successfully matched line and the three lines following that line

$ grep -A 3 -i "example" demo_text
Copy the code

Recursively queries files in a folder that contain the specified string

$ grep -r "ramesh" *
Copy the code

3. find

Find a file with the specified filename (case insensitive)

$ find -iname "MyProgram.c"
Copy the code

Execute a command on the found file

$ find -iname "MyProgram.c" -exec md5sum {} \;
Copy the code

Find all empty files in the home directory

$ find ~ -empty
Copy the code

4. ssh

Log in to the remote host

$ ssh -l jsmith remotehost.example.com
Copy the code

Example Debugging the SSH client

$ ssh -v -l jsmith remotehost.example.com
Copy the code

The SSH client version is displayed

$ ssh -V
Copy the code

5. sed

When you copy a file from Dos to Unix/Linux, each line of the file will end with \r\n. Sed can easily convert it to a UNIx-formatted file with \n ending

$ sed 's/.$//' filename
Copy the code

Reverse file contents and print

$ sed -n '1! G; h; p' filename
Copy the code

Adds a line number for a non-empty line

$ sed '/. / =' thegeekstuff.txt | sed 'N; s/\n/ /'
Copy the code

6. awk

Delete duplicate rows

$ awk '! ($0 in array) { array[$0]; print}' temp
Copy the code

Print all lines in /etc/passwd that contain the same Uids and Gids

$ awk -F ':' '$3 = $4' /etc/passwd
Copy the code

Prints the fields of the specified part of the file

$ awk '{print $2,$5; } ' employee.txt
Copy the code

7. vim

Open the file and skip to line 10

$ vim +10 filename.txt
Copy the code

Open the file and jump to the first matching line

$ vim +/search-term filename.txt
Copy the code

Open the file in read-only mode

$ vim -R /etc/passwd
Copy the code

8. diff

Ignore whitespace characters when comparing

$ diff -w name_list.txt name_list_new.txt
Copy the code

9. sort

Sort file contents in ascending order

$ sort names.txt
Copy the code

Sort file contents in descending order

$ sort -r names.txt
Copy the code

Sort the contents of /etc/passwd with the third field

$ sort -t: -k 3n /etc/passwd | more
Copy the code

10. export

Output the environment variable that matches 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"Copy the code

Set global environment variables

$export ORACLE_HOME = / u01 / app/oracle/product / 10.2.0Copy the code

11. xargs

Copy all image files to the external drive

$ ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
Copy the code

Compress and package all JPD files in the system

$ find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
Copy the code

Download pages corresponding to all urls listed in the file

$cat url - list. TXT | xargs wget - cCopy the code

12. ls

Display file size in a readable way (displayed as MB,GB…)

$ ls -lh
-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz
Copy the code

Lists files in ascending order from last modified time

$ ls -ltr
Copy the code

Displays the file type after the file name

$ ls -F
Copy the code

13. pwd

Output the current working directory

14. cd

CD – You can switch between the two most recently working directories

Use shopt -s cdspell to set up automatic spell checking of CD commands

15. gzip

Create a *.gz zip file

$ gzip test.txt
Copy the code

Decompress the *. Gz file

$ gzip -d test.txt.gz
Copy the code

Displays the ratio of compression

$gzip -l *. Gz compressed in ratio uncompressed_name 23709 97975 asp-patch-rpms.txtCopy the code

16. bzip2

Create a *.bz2 zip file

$ bzip2 test.txt
Copy the code

Unzip the *.bz2 file

bzip2 -d test.txt.bz2
Copy the code

17. uzip

Unzip the *.zip file

$ unzip test.zip
Copy the code

View the contents of the *.zip file

$ unzip -l jasper.zip
Archive:  jasper.zip
Length     Date   Time    Name
--------    ----   ----    ----
40995  11-30-98 23:50   META-INF/MANIFEST.MF
32169  08-25-98 21:07   classes_
15964  08-25-98 21:07   classes_names
10542  08-25-98 21:07   classes_ncomp
Copy the code

18. shutdown

Shut down the system and shut down immediately

$ shutdown -h now
Copy the code

Power off after 10 minutes

$ shutdown -h +10
Copy the code

restart

$ shutdown -r now
Copy the code

Forcibly perform a system check during the restart

$ shutdown -Fr now
Copy the code

19. ftp

The FTP command is similar to the SFTP command. Connect to the FTP server and download multiple files

$ ftp IP/hostname
ftp> mget *.html
Copy the code

Displays a list of files on the remote host

ftp> mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html
Copy the code

20. crontab

View the crontab entry for a user

$ crontab -u john -l
Copy the code

Set a scheduled task to be performed every 10 minutes

*/10 * * * * /home/ramesh/check-disk-space
Copy the code

21. service

The service command is used to run the System V init script, which is usually located in the /etc/init.d file. This command can directly run the scripts in this folder without adding a path

Viewing Service Status

$ service ssh status
Copy the code

View the status of all services

$ service --status-all
Copy the code

Restart the service

$ service ssh restart
Copy the code

22. ps

The ps command is used to display information about processes that are running. The ps command has many options, only a few of which are listed here

View all processes that are currently running

$ ps -ef | more
Copy the code

Displays the currently running processes in a tree structure, with the H option indicating the hierarchy of processes

$ ps -efH | more
Copy the code

23. free

This command displays the current memory usage of the system, including used memory, available memory, and swap memory

By default, free prints the memory usage in bytes

$ free
             total       used       free     shared    buffers     cached
Mem:       3566408    1580220    1986188          0     203988     902960
-/+ buffers/cache:     473272    3093136
Swap:      4000176          0    4000176
Copy the code

If you want to output the amount of memory used in other units, you need to add an option -g for GB, -m for MB, -k for KB, and -b for bytes

$ free -g
             total       used       free     shared    buffers     cached
Mem:             3          1          1          0          0          0
-/+ buffers/cache:          0          2
Swap:            3          0          3
Copy the code

If you want to see a summary of all memory, use the -t option, which adds a summary line to the output

ramesh@ramesh-laptop:~$ free -t
             total       used       free     shared    buffers     cached
Mem:       3566408    1592148    1974260          0     204260     912556
-/+ buffers/cache:     475332    3091076
Swap:      4000176          0    4000176
Total:     7566584    1592148    5974436
Copy the code

24. top

The top command will display the most resource-consuming processes on the system (sorted by CPU usage by default). If you want to change the sorting method, you can click O (capital O) in the result list to display all columns available for sorting. At this point, you can select the columns you want to sort

Current Sort Field:  P  for window 1:Def
Select sort field via field letter, type any other key to return
Copy the code

a: PID = Process Id v: nDRT = Dirty Pages count d: UID = User Id y: WCHAN = Sleeping in Function e: USER = User Name z: Flags = Task Flags… \

If you want to display only the processes of a particular user, use the -u option

$ top -u oracle
Copy the code

25. df

Displays the disk usage of the file system. By default, df -k displays the 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
Copy the code

Use the -h option to display disk usage in a more readable manner

$ 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 // Volumes/publicCopy the code

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
Copy the code

26. kill

The kill command is used to terminate a process. We usually use ps -ef to find the process number, and then use kill -9 to terminate the process. You can also use killall, pkill, and xkill to killa process

$ ps -ef | grep vim
ramesh    7243  7222  9 22:43 pts/2    00:00:00 vim
Copy the code

$ kill -9 7243\

27. rm

Confirm before deleting a file

$ rm -i filename.txt
Copy the code

Using shell metacharacters in file names can be very useful. Print and confirm the file name before deleting the file

$ rm -i file*
Copy the code

Recursively delete all files under the folder and delete the folder

$ rm -r example
Copy the code

28. cp

Copy file 1 to file 2 with the permissions, owner, and timestamp of the file

$ cp -p file1 file2
Copy the code

Copy file1 to file2. If file2 exists, it prompts whether to overwrite

$ cp -i file1 file2
Copy the code

29. mv

Rename the file name file1 to file2, prompting whether to overwrite if file2 exists

$ mv -i file1 file2
Copy the code

Note If the -f option is used, no prompt is displayed

-v prints the renaming process, which is handy if the file name contains wildcards

$ mv -v file1 file2
Copy the code

30. cat

You can view the contents of more than one file at a time. The following command prints the contents of file1 and then file2

$ cat file1 file2
Copy the code

The -n command prefixes each line with a line number

$ cat -n /etc/logrotate.conf
    1	/var/log/btmp {
    2	    missingok
    3	    monthly
    4	    create 0660 root utmp
    5	    rotate 1
    6	}
Copy the code

31. mount

To mount a file system, you need to create a directory and mount the file system to the directory

# mkdir /u01
Copy the code

# mount /dev/sdb1 /u01\

You can also add it to fstab for automatic mounting, so that the file system will be loaded whenever the system restarts

/dev/sdb1 /u01 ext2 defaults 0 2
Copy the code

32. chmod

Chmod is used to change file and directory permissions

Give all permissions (including read, write, and execute) to the owner and owner group of the specified file.

$ chmod ug+rwx file.txt
Copy the code

Deletes all permissions for the specified file’s owner group

$ chmod g-rwx file.txt
Copy the code

Change the permissions of a directory, and recursively change the permissions of all files and subdirectories under the directory

$ chmod -R ug+rwx file.txt
Copy the code

33. chown

Chown is used to change the file owner and file group

Change the owner of a file to Oracle and the owner group to DB

$ chown oracle:dba dbora.sh
Copy the code

Use the -r option to recursively modify directories and files under directories

$ chown -R oracle:dba /home/oracle
Copy the code

34. passwd

The passwd command is used to change the password on the cli. Using this command, you are required to enter the old password first and then the new password

$ passwd
Copy the code

The super user can use this command to change the password of another user without entering the user password

# passwd USERNAME
Copy the code

The passwd command can also delete the password of a user. Only the root user can run this command. After the password is deleted, the user can log in to the system without entering a password

# passwd -d USERNAME
Copy the code

35. mkdir

Create a directory named temp under the home directory

$ mkdir ~/temp
Copy the code

Use the -p option to create all nonexistent directories on a path

$ mkdir -p dir1/dir2/dir3/dir4/
Copy the code

36. ifconfig

Ifconfig is used to view and configure network interfaces in the Linux operating system

View all network interfaces and their status

$ ifconfig -a
Copy the code

Use the up and down commands to start or stop an interface

$ ifconfig eth0 up
Copy the code

$ ifconfig eth0 down\

37. uname

Uname can display important system information, such as kernel name, host name, kernel version number, processor type, and so on

$ uname -a
Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux
Copy the code

38. whereis

You can use whereis when you don’t know the location of a command. Find the location of ls using whereis

$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls1..gz /usr/share/man/man1p/ls.1p.gz
Copy the code

When you want to find the location of an executable program that is not in the default directory of whereis, you can use the -b option and specify the directory as an argument to this option. The following command looks for LSMK commands in the/TMP directory

$ whereis -u -B /tmp -f lsmk
lsmk: /tmp/lsmk
Copy the code

39. whatis

Wathis displays the description of a command

$ whatis ls
ls		(1)  - list directory contents
Copy the code

$ whatis ifconfig

ifconfig (8) – configure a network interface\

40. locate

The locate name shows the path to a given file (or set of files), using the database created by UpdatedB

The following command will display all the files in the system that contain the crontab string

$ locate crontab
/etc/anacrontab
/etc/crontab
/usr/bin/crontab
/usr/share/doc/cron/examples/crontab2english.pl.gz
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim72/syntax/crontab.vim
Copy the code

41. man

Displays the MAN page for a command

$ man crontab
Copy the code

Some commands may have multiple MAN pages, with each MAN page corresponding to a command type

$ man SECTION-NUMBER commandname
Copy the code

The MAN page can be divided into eight command types

  1. User commands
  2. The system calls
  3. C library functions
  4. Interface between device and network
  5. The file format
  6. Games and screensavers
  7. Environment, tables, macros
  8. System administrator commands and background running commands

For example, if we execute whatis crontab, you can see that crontab has two command types 1 and 5, so we can view the man page of command type 5 by using the following command

$ whatis crontab
crontab (1)          - maintain crontab files for individual users (V3)
crontab (5)          - tables for driving cron
Copy the code

$ man 5 crontab\

42. tail

By default, the tail command displays the last 10 lines of the file

$ tail filename.txt
Copy the code

You can specify the number of lines to display using the -n option

$ tail -n N filename.txt
Copy the code

You can also view in real time using the -f option, which waits until a new line is added to the end of the file and continues to output new lines. This option is very useful when viewing logs. You can terminate the command by ctrl-c

$ tail -f log-file
Copy the code

43. less

This name can display the contents of a file without loading the entire file, which is very useful when viewing large log files

$ less huge-log-file.log
Copy the code

When you open a file with less, the following two keys will help you a lot. They are used for scrolling forward and backward

CTRL+F - Backward one window CTRL+B - Backward one windowCopy the code

44. su

The su command is used to switch user accounts. The super user can switch to any other user without entering a password

$ su - USERNAME
Copy the code

Execute a command under a different username. In the following example, user John executes the ls command using the raj username and returns John’s account

[john@dev-server]$ su - raj -c 'ls'
Copy the code

[john@dev-server]$\

Log in as the specified user and use the specified shell program instead of the default

$ su -s 'SHELLNAME' USERNAME
Copy the code

45. mysql

Mysql is probably the most widely used database on Linux. Even if you don’t have mysql installed on your server, you can use mysql clients to connect to remote mysql servers

To connect to a remote database, you need to enter a password

$ mysql -u root -p -h 192.168.1.2
Copy the code

Connecting to a Local Database

$ mysql -u root -p
Copy the code

You can also enter the database password on the command line by using -p as the password argument. You can write the password after p without Spaces

46. yum

Install Apache using yum

$ yum install httpd
Copy the code

Update apache

$ yum update httpd
Copy the code

Uninstall/Delete Apache

$ yum remove httpd
Copy the code

47. rpm

Install Apache using RPM

# RPM - the ivh HTTPD - then - 22.0.1. El5.. I386 RPMCopy the code

Update apache

# RPM - uvh HTTPD - then - 22.0.1. El5.. I386 RPMCopy the code

Uninstall/Delete Apache

# rpm -ev httpd
Copy the code

48. ping

Ping a remote host to send only five packets

$ ping -c 5 gmail.com
Copy the code

49. date

Set the system date

# date -s "01/31/2010 23:59:53"
Copy the code

When you change the system time, you need to synchronize hardware time with system time

# hwclock --systohc -- UTCCopy the code

50. wget

Use WGET to download software, music, and videos from the Web

$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
Copy the code

Download the file and save it with the specified file name

$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701
Copy the code