Linux common commands (latest version in 2022)

1. ls command

It is the abbreviation of list. Through ls command, you can not only view the files contained in the linux folder, but also view the file permissions (including directory, folder and file permissions)

View directory information, etc.

ls -a List all files in the directory, including.Start hidden file
ls -A List Division.and..Other documents
ls -r Reverse order arrangement
ls -t Sort by file modification time
ls -S Sort by file size
ls -h Display in readable size
ls -l In addition to the file name, the file permission, owner, file size and other information are listed in detail

2. cd command

Description: switch the current directory to dirName.

cd(changeDirectory) command syntax:

cd [Directory name]

3. pwd command

The pwd command is used to view the current working directory path.

4. mkdir command

The mkdir command is used to create folders.

Available options:

  • -m: Set the access permission for the new directory, or use the chmod command;
  • -p: Can be a path name. At this time, if some directories in the path do not exist, after adding this option, the system will automatically establish those directories that are not yet in existence, that is, multiple directories can be established at one time.

5. rm command

Delete one or more files or directories in a directory. If the - r option is not used, rm will not delete the directory. If you use rm to delete a file, you can usually still restore the file to its original state.

6. rmdir command

To delete one or more subdirectory entries from a directory, you must also have write permission to its parent directory when deleting a directory.

7. mv command

Move the file or modify the file name according to the second parameter type (e.g. directory, move the file; if it is a file, re command the file).

When the second parameter is a directory, the first parameter can be multiple files or directories separated by spaces, and then move multiple files specified by the first parameter to the directory specified by the second parameter.

8. cp command

Copy source files to destination files, or copy multiple source files to destination directories.

Note: when copying from the command line, if the target file already exists, you will be prompted whether to overwrite it. In the shell script, if you do not add the - i parameter, you will not be prompted, but directly overwrite it!

9. cat command

cat has three main functions:

1. Display the entire file at once:

cat filename

2. Create a file from the keyboard:

cat > filename

Only new files can be created, and existing files cannot be edited.

3. Merge several documents into one document:

cat file1 file2 > file
  • -b output line number for non null
  • -n output all line numbers

10. more command

The function is similar to cat. More will be displayed page by page to facilitate users to read page by page. The most basic instruction is to press the space key to display the next page, and press the b key to display back one page.

  • Command parameters:
+n      From the first n Line start display
-n       Define screen size as n that 's ok
+/pattern Search for the string before each file is displayed( pattern),Then start the display after the first two lines of the string 
-c       Clear the screen from the top and display
-d       Hint“ Press space to continue,'q' to quit(Press spacebar to continue, press q Press exit to disable the ringing function
-l        ignore Ctrl+l(Page feed) character
-p       Page feed the file by clearing the window instead of scrolling, and-c Similar options
-s       Displays consecutive blank lines as one line
-u       Remove the underline from the contents of the document
  • Common operation commands
Enter    down n Line, need to be defined. The default is 1 line
Ctrl+F   Scroll down one screen
 Space bar scroll down one screen
Ctrl+B  Return to previous screen
=       Output the line number of the current line
:f     The output file name and the line number of the current line
V      call vi editor 
!Command call Shell,And execute the command
q       sign out more

11. less command

Less is similar to more, but with less, you can browse files at will, while more can only move forward, but not backward, and less will not load the whole file before viewing.

You can use n to see the next one and p to see the previous one.

Common command parameters:

-i  Ignore case when searching
-N  Displays the line number of each line
-o  <file name> take less The output content is saved in the specified file
-s  Displays a row of consecutive empty rows
/String: the ability to drill down to "string"
?String: the ability to search up for "string"
n: Repeat the previous search (and / or ? (related)
N: Reversely repeats the previous search (and / or ? (related)
-x <number> Will“ tab"The keys are displayed as specified numeric spaces
b  Turn back one page
d  Turn back half a page
h  Display help interface
Q  sign out less command
u  Scroll forward half a page
y  Scroll forward one line
 Space bar to scroll one line
 Press enter to scroll through the page
[pagedown]:  Turn down one page
[pageup]:    Turn up a page

12. head command

Head is used to display the beginning of the file to the standard output. The default head command prints the first 10 lines of its corresponding file.

Common parameters:

-n<Number of rows> Number of rows displayed (the number of rows is a complex number, indicating the number from the last to the front)

13. tail command

Used to display the contents at the end of the specified file. When the file is not specified, it is processed as input information. Common view log files.

Common parameters:

-f Circular reading (often used to view incremental log files)
-n<Number of rows> Display rows (back to front)

14. which command

To find a file in linux, but do not know where it is, you can use the following commands to search:

which     View the location of the executable.
whereis View the location of the file.
locate  Check the file location with the database.
find        Actually search the hard disk to query the file name.

Which is to search the location of a system command in the PATH specified, and return the first search result. Using the which command, you can see whether a system command exists and where the command is executed.

Common parameters:

-n  Specify the length of the file name. The specified length must be greater than or equal to the longest file name in all files.

15. chmod command

Used to change the access rights of linux system files or directories. Use it to control access to files or directories. This command has two uses. One is the text setting method including letters and operator expressions; The other is the digital setting method including numbers.

16. chown command

chown changes the owner of the specified file to the specified user or group. The user can be a user name or user ID; A group can be a group name or a group ID; Files are separated by spaces. The list of files to change permissions supports wildcards.

-c Displays information about the changed section
-R Process specified directories and subdirectories

17. df command

Displays disk space usage. Obtain information such as how much space the hard disk is occupied and how much space is left. If no file name is specified, the available space of all currently mounted file systems will be displayed. By default, disk space is displayed in 1KB unless the environment variable POSIXLY_CORRECT is specified so that it will be displayed in 512 bytes:

-a List of all file systems
-h Display information in a readable manner
-i display inode information
-k The block is 1024 bytes
-l Show only local disks
-T List file systems

18. du command

The du command is also used to view the space used, but unlike the df command, the Linux du command is used to view the space used by files and directory disks:

Command format:

du [option] [file]

Common parameters:

-a Displays the size of all files in the directory
-k with KB Displays the file size in units
-m with MB Displays the file size in units
-g with GB Displays the file size in units
-h Displays the file size in a readable manner
-s Show totals only
-c or--total  In addition to displaying the size of individual directories or files, it also displays the sum of all directories or files

19. ln command

The function is to establish a synchronous link for files in another location. When this problem is needed in different directories, it is not necessary to create the same file for each directory, and the disk consumption is reduced through the link created by ln.

Link classification: software link and hard link

Soft link:

  • 1. Soft links exist in the form of paths. Similar to shortcuts in Windows operating system
  • 2. Soft links can cross file systems, but hard links cannot
  • 3. Soft link can link a nonexistent file name
  • 4. Soft link can link the directory

Hard link:

  • 1. Hard link, in the form of document copy. But it does not occupy the actual space.
  • 2. Hard links to directories are not allowed
  • 3. Hard links can only be created in the same file system


  • First, the ln command will keep the synchronization of each linked file, that is, no matter where you change, other files will change the same;
  • Second: ln links can be divided into soft links and hard links. Soft links are ln – s source files and target files. They will only generate a file image at the location you select and will not occupy disk space. Hard links ln source files and target files. Without the parameter - s, they will generate a file with the same size as the source file at the location you select, Whether it is soft link or hard link, the file changes synchronously.
  • Third: the ln instruction is used to link files or directories. If more than two files or directories are specified at the same time, and the last destination is an existing directory, all the files or directories specified above will be copied to the directory. If multiple files or directories are specified at the same time, and the last destination is not an existing directory, an error message will appear.

Common parameters:

-b Delete, overwrite previously established links
-s Soft link (symbolic link)
-v Show detailed processing

20. date command

Displays or sets the date and time of the system.

Command parameters:

-d<character string>  Displays the date and time to which the string refers. The string must be preceded by double quotes.
-s<character string>  Set the date and time based on the string. The string must be preceded by double quotes.
-u  display GMT. 
%H hour(00-23)
%I hour(00-12)
%M minute(With 00-59 To represent)
%s Total seconds. The starting time is 1970-01-01 00:00:00 UTC. 
%S second(Expressed in local idioms)
%a Abbreviation for week.
%A Full name of the week.
%d date(With 01-31 To represent). 
%D date(Including mm / DD / yy). 
%m month(With 01-12 To represent). 
%y particular year(With 00-99 To represent). 
%Y particular year(Expressed in four digits). 

21. cal command

You can display the Gregorian calendar (Gregorian calendar). If there is only one parameter, it indicates the year (1-9999). If there are two parameters, it indicates the month and year:

Common parameters:

-3 Displays the calendar of the previous month, the current month and the next month
-m Displays Monday as the first column
-j Displays the day ordinal of the current year
-y [year]Displays the current year[year]Calendar of copies

22. grep command

Powerful text search command, grep(Global Regular Expression Print) global regular expression search.

grep works by searching for string templates in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are regarded as file names. The search results are sent to the standard output without affecting the content of the original file.

Command format:

grep [option] pattern file|dir

Common parameters:

-A n --after-context After displaying matching characters n that 's ok
-B n --before-context Before displaying matching characters n that 's ok
-C n --context Display before and after matching characters n that 's ok
-c --count Calculate the number of columns that match the style
-i ignore case
-l Lists only file names whose file contents conform to the specified style
-f Read keywords from file
-n Displays the number of lines in the file where the matching content is located
-R Recursively find folders

Regular expression for grep:

^  #The beginning of the anchor line, such as' ^ grep 'matches all lines beginning with grep. 
$  #Anchor the end of the line, such as: 'grep $' matches all lines ending in grep. 
.  #Matches a non newline character, such as: 'gr.p' matches GR followed by any character, followed by P.  
*  #Matches zero or more previous characters, such as' * grep 'matches all one or more spaces followed by the line of grep.
.*   #Used together to represent any character.  
[]   #Matches characters within a specified range, such as' [Gg]rep 'matches grep and grep. 
[^]  #Matches a character that is not within the specified range, such as' [^ A-FH-Z]rep 'matches a line that starts with a letter that does not contain A-R and T-Z, followed by Rep.  
\(..\)  #Mark matching characters, such as' \ (love \) ', and love is marked as 1.   
\<      #Anchor the beginning of the word, such as' \ < grep 'matches the line containing the word beginning with grep.
\>      #Anchor the end of the word, such as' grep \ > 'to match the line containing the word ending in grep.
x\{m\}  #Repeat the character x, m times, for example: '0 \ {5 \}' matches the line containing 5 o's. 
x\{m,\}  #Repeat the character x at least m times. For example, 'o\{5, \}' matches a line with at least 5 o's.  
x\{m,n\}  #Repeat the character x at least m times and no more than n times. For example, 'o\{5,10 \}' matches 5-10 o lines.  
\w    #Match text and numeric characters, i.e. [A-Za-z0-9], for example, 'G\w*p' matches G followed by zero or more text or numeric characters, followed by P.  
\W    #\The inverted form of w matches one or more non word characters, such as period, period, etc.  
\b    #Word locks, such as' \ bgrep\b 'only match grep.

23. wc command

wc(word count) function is to count the number of bytes, words and lines in the specified file, and output the statistical results

Command format:

wc [option] file..

Command parameters:

-c Count bytes
-l Count rows
-m Count characters
-w Count the number of words. A word is defined as a string separated by blank, skip, or newline characters

24. ps command

ps(process status) is used to view the current running process status. It can be viewed at one time. If dynamic continuous results are required, use top

Processes on linux have five states:

  • \1. Run (running or waiting in the run queue)
  • \2. Interrupt (in sleep, blocked, waiting for the formation of a condition or receiving a signal)
  • \3. Non interruptible (the process cannot wake up and run after receiving the signal, and the process must wait until an interrupt occurs)
  • \4. Dead (the process has been terminated, but the process descriptor exists until it is released after the parent process calls wait4() system call)
  • \5. Stop (the process stops running after receiving sigstop, sigstp, sigtin and sigtou signals)

The ps tool identifies five status codes of the process:

D Non interruptible uninterruptible sleep (usually IO)
R function runnable (on run queue)
S interrupt sleeping
T stop it traced or stopped
Z dead a defunct ("zombie") process

Command parameters:

-A Show all processes
a Show all processes
-a Display all processes under the same terminal
c Displays the real name of the process
e Show environment variables
f Show relationships between processes
r Displays the processes running on the current terminal
-aux Displays all processes that contain other uses

25. top command

Displays information about the process currently being executed by the system, including process ID, memory utilization, CPU utilization, etc

Common parameters:

-c Displays the complete process command
-s Confidentiality mode
-p <Process number> Specify process display
-n <frequency>Cycle display times

26. kill Command

Send the specified signal to the corresponding process. If the model is not specified, SIGTERM (15) will be sent to terminate the specified process. If the program cannot be terminated, the "- KILL" parameter can be used. The signal sent by it is SIGKILL(9), which will force the end of the process. Use the ps command or jobs command to view the process number. The root user will affect the user's process, and the non root user can only affect his own process.

Common parameters:

-l  Signal. If the number parameter of the signal is not added, it is used“-l"The parameter lists all signal names
-a  When processing the current process, the correspondence between command name and process number is not limited
-p  appoint kill The command prints only the process number of the related process without sending any signal
-s  Specify send signal
-u  Designated user

27. free command

Displays the system memory usage, including physical memory, swap memory, and kernel buffer memory.

Command parameters:

-b with Byte Display memory usage
-k with kb Displays memory usage in units
-m with mb Displays memory usage in units
-g with gb Displays memory usage in units
-s<Interval seconds> Continuous display memory
-t Displays the total memory usage

Keywords: Linux shell bash server

Added by severndigital on Wed, 05 Jan 2022 09:17:47 +0200