Shell Common commands

Source: Internet
Author: User
Tags call shell echo command

Shell Common commands

1. ls: similar to the dir command in dos

The most common parameters of ls include-a-l-F.


Linux Files. files starting with ls are regarded as hidden files and cannot be seen only by using ls commands. In addition to displaying common file names, ls-a can also display hidden files.


This parameter displays more detailed file information.


This parameter is used to add symbols of the file type after a file. For example, * Indicates executable,/indicates directory, and @ indicates link file, this is because the-F parameter is used. But now, basically all Linux releases have built-in the-F parameter for ls. That is to say, we can see all kinds of resolution symbols without entering this parameter.

2. cd: used to switch the current working directory of the user

Cd aaa enters the aaa directory

If the directory is not specified after the cd command, the current user's home directory will be switched.

Cd ~ Switch to the home Directory of the current user without specifying a directory after cd.

After the cd-command is followed by a minus sign, it is returned to the directory before the switch.

Cd .. return to the upper-level directory under the current directory

3. pwd: used to display the current working directory of the user

4. mkdir and rmdir

Midir: create directory rmdir: delete directory

Both commands support the-p parameter. For the mkdir command, if the parent directory of the specified path does not exist, the mkdir command is created together. For the rmdir command, all levels of directories in the specified path are deleted. If the folder contains content, you cannot use the rmdir command.

As follows:

Mkdir-p 1/2/3

Rmdir-p 1/2/3

5. cp: Copy command

Copy a file to another directory: cp 1.txt ../test2

Copy a file to the local directory and rename it: cp 1.txt 2.txt

Copy a folder and change it to B: cp-r a B

6. mv: Mobile Command

Move one file to another directory: mv 1.txt ../test1

Rename a file in this directory: mv 1.txt 2.txt

Upload a file to another directory and rename it: mv 1.txt ../test1/2.txt

7. rm command

The rm command is used to delete files. Similar to the del/erase command in dos, the common parameters of the rm command include-I,-r, and-f.

-I: before deleting a file, the system will first ask for confirmation. After you press enter, the file will be deleted. Note that files deleted in linux cannot be recovered. Therefore, be cautious when deleting files.

-R: This parameter supports directory deletion, which is similar to the rmdir command.

-F: opposite to the-I parameter.-f indicates force deletion.

8. du and df commands

The du command displays the disk space occupied by the current directory, and the df command displays the remaining disk space.

If the du command does not add any parameters, the system returns the usage of the entire disk. If a directory is added to the disk, the usage of the directory is used on the disk.

Du-hs specifies the directory to view the total size of the specified directory

Du-hs./* view the size of all folders and files in the current directory

Both commands support the-k,-m, and-h parameters. The-k and-m parameters are similar, indicating the display unit. One is k bytes and the other is MB, -h indicates human-readable, which is a user-friendly display mode.

9. cat command

The cat command is used to display or link common ascii text files. Cat is short for concatenate, similar to the type command under dos. The usage is as follows:

Cat file1: Display file1 File Content

Cat file1 file2 displays file1 and file2 in sequence

Cat file1 file2> file3 combines the content of file1 and file2, and then "Redirect (>)" to the file3 file.

">" Is the right redirection character, indicating that the result of the command on the left is used as the input of the command on the right. Note: if the file on the right is an existing file, its original content will be cleared, the left-side command output content. If you want to write data in append mode, use the ">" redirection character instead.

If no file is specified on the left, such as cat> file1, after the input is complete, press [Ctrl] + [c] or [Ctrl] + [d] to write the user's input content to file1.

10. echo command

The frequency of Using echo commands is not less than ls and cat, especially in shell script writing.

Syntax: echo [-ne] [String]

Function: echo sends the input string to the standard output. The output string is separated by blank characters and a line break is added at the end.


-N: the last line break when the string is displayed

-E supports escape characters in the following formats.-E does not support escape characters in the following formats.

/A sends an alert;

/B. Delete the previous character;

/C does not end with a line break;

/F line feed, but the cursor remains at the original position;

/N wrap and move the cursor to the beginning of the line;

/R move the cursor to the beginning of the line without line breaks;

/T Insert the tab;

/V is the same as/f;

// Insert/character;

/Nnn inserts the ASCII characters represented by nnn (octal;


Oracle @ hjtest :~ /Hgd> echo "123" "456"

123 456

Oracle @ hjtest :~ /Hgd> echo "123/n456"


Oracle @ hjtest :~ /Hgd> echo-e "123/n456"



Oracle @ hjtest :~ /Hgd> echo-E "123/n456"


Oracle @ hjtest :~ /Hgd> echo-E "123 // 456"

123 // 456

Oracle @ hjtest :~ /Hgd> echo-e "123 // 456"


Oracle @ hjtest :~ /Hgd & gt; echo-e "123/100456"

123 @ 456


In bash used in Linux, single quotation marks ('') and double quotation marks are different. single quotation marks ignore all escape characters. Double quotation marks do not ignore the following special characters:

Dollar signs ($), Back quotes ('), Backslashes (/), Excalmatory mark (!)


Oracle @ hjtest: ~> Echo "'test '"

-Bash: TEST: command not found

Oracle @ hjtest: ~> Echo ''test''


Oracle @ hjtest: ~> Echo "$ TEST"

Oracle @ hjtest: ~> Echo '$ Test'


Oracle @ hjtest: ~> Echo "// TEST"


Oracle @ hjtest: ~> Echo '// Test'


Oracle @ hjtest: ~> Echo "Hello! "

Echo "Hello"


Oracle @ hjtest: ~> Echo 'Hello! '


11. more, less, clear

Nmore, less command

These two commands are used to view files. If a file is too long to display more than one screen, you can only view the last content using the cat command. You can view the content by page using the more and less commands. The more command pauses the display of the object content that exceeds one page by page. The next page is displayed only after you press the button. In addition to more functions, less can also use the direction keys to scroll up or down the file for more convenient reading.

Less common action commands:

The enter key moves down a row;

Y: move one row up;

The Space key scrolls down a screen;

B. Scroll up the screen;

D. Scroll down the half screen;

H less help;

U moves to the ocean half screen;

W specifies the row to be displayed, which is displayed from the next row of the specified number. For example, if the row is 6, the row is displayed from the second row;

G jumps to the first line;

G to the last line;

P n % jumps to n %, for example, 10%, that is, it is displayed starting from 10% of the entire file content;

/Pattern: Search for pattern. For example,/MAIL indicates searching for MAIL words in files;

V call the vi editor;

Q: Exit less

! Command to call SHELL, you can run commands; for example! Ls displays all files in the current directory of the current column;

Nclear command

The clear command is used to clear the current screen without any parameters. It is the same as the cls command in dos.

12. head and tail

The nhead and tail commands are used to view text files. The difference is that the head displays n lines of the file header and the tail displays n lines of the file. By default, n is 10 lines. You can specify the number of rows in the-n mode, for example:

Head-100 file and tail-100 file indicate the content of the first 100 lines and the last 100 lines respectively.

The ntail-f command allows you to view new content in the file in real time.

13. wc command

This command is used to count the number of bytes, number of words, and number of lines in a specified file. The options of this command are as follows:

-L number of statistics rows

-W words

-C: count the number of bytes.

These options can be used in combination. The order and number of output columns are not affected by the order and number of options. It is always displayed in the following order and each item has at most one column.

Number of lines, number of words, number of bytes, and file name

If there is no file name in the command line, no file name appears in the output.

For example:

Oracle @ hjtest: ~> Wc 1.txt 2.txt

4601679 16353 1.txt

3001095 10665 2.txt

7602774 total usage

Oracle @ hjtest: ~> Wc-l 1.txt

460 1.txt

The default parameter is-lcw, that is, the execution result of wc file1 file2 is the same as that above.

14. grep command

Grep is the abbreviation of (global search regular expression (RE) and print out the line. It is used to search and print the rows containing the specified mode from the file, it is a powerful text search tool that supports searching text using regular expressions. Grep works like this. it searches for string templates in one or more files. If the template contains spaces, it must be referenced by "". All strings after the template are treated as file names. The search result is sent to the screen without affecting the content of the original file.

Grep can be used in shell scripts because grep returns a status value to indicate the search status. If the template search is successful, 0 is returned. If the search is unsuccessful, 1 is returned, if the searched file does not exist, 2 is returned. We can use these return values to automate text processing.


$ Ls-l | grep '^'

Filter the ls-l output content in the MPs queue and display only the rows starting with.

$ Grep 'test' d *

Display all the lines containing test in files starting with d.

$ Grep 'test' aa bb cc

The row Matching test is displayed in the aa, bb, and cc files.

$ Grep '[a-z]/{5/} 'aa

Display All rows of a string that contains at least five consecutive lowercase characters.

$ Grep 'w/(es/) t. */1 'aa

If west is matched, es is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another es (/1). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to escape the "/" number and write it as 'W (es) t. */1.

15. man and logout commands

Man command

Man is the abbreviation of manual, which is equivalent to the online Help of Unix/Linux. Every system command and call has a very detailed description, and most of them are in English. For example, man ls is used to view the instructions for using the ls command. In general, there is another way to view help, such as ls-help, which is supported by most commands.

Logout command

This command is used to exit the system, which corresponds to the login command.

16. Pipelines and xargs
MPs queue:

The pipeline operator "|" provided by Linux is used to separate the two commands. The output of the command on the left of the pipeline operator is used as the input of the command on the right of the pipeline operator. The continuous use of pipelines means that the output of the first command will be used as the input of the second command, and the output of the second command will be used as the input of the Third Command, and so on.

Note: The command input on the left of the pipeline serves as the command input on the right of the pipeline (the command input is certain), not a parameter. Not all commands support pipelines.

Example: ls | grep a check the file or folder named a in the current directory


Most Linux commands generate output: file list, string list, and so on. But what if I want to use another command and use the output of the previous command as a parameter? For example, the file command displays the file type (Executable file, ascii text, etc.); you can process the output so that it only displays the file name, currently, you want to pass these names to the ls-l command to view timestamps. The xargs command is used to complete this task.
Note: The find command passes the matching file to the xargs command, while the xargs command only obtains a part of the file at a time, instead of all, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this


1. Search for the memory information dumping file (core dump) in the entire system, and save the result to the/tmp/core. log file:

$ Find/-name "core"-print | xargs echo "">/tmp/core. log

2. When there are too many files in a directory, using the rm * command directly will cause the package to be too long. You can delete all the files as follows:

$ Ls | xargs rm

17. basename and dirname

Basename is used to view the name of a file without a path, and dirname is used to view the file path. We can test the effect to see it:

> Basename/home/hj/1.txt

1. txt

> Dirname/home/hj/1.txt


> Basename 1.txt

1. txt

> Dirname 1.txt


18. The set command without any parameters will display all shell Variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.