Parameters you are not familiar with in common shell commands

Source: Internet
Author: User
Tags call shell echo command

          

1. ls: similar to the Dir command in DOS

The most common parameters of LS include-a-l-f.

Ls-

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.

Ls-l

This parameter displays more detailed file information.

Ls-F

This parameter is used to add symbols of the file type after a file. For example* Indicates executable,/indicates directory, and @ indicates link fileThis 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.

-

Same as-a, but does not list "." (indicating the current directory) and "." (indicating the parent directory of the current directory ).

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 will be returned to the directory before the switch-this is more practical, but most people do not know

 

 

3. 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

 

4. 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.

 

5. 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.

 

6. 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.

 

7. 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.

Parameters:

-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;Note: It is a backslash.

\ 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 tab;

\ V is the same as/F;

\ Insert \ characters;

\ NNN inserts the ASCII characters represented by NNN (octal;

 

Example(The following example should be used as a backslash)

[Email protected]: ~ /Hgd> echo "123" "456"

123 456

[Email protected]: ~ /Hgd> echo "123/n456"

123/n456

[Email protected]: ~ /Hgd> echo-e "123/n456"

123

456

[Email protected]: ~ /Hgd> echo-e "123/n456"

123/n456

[Email protected]: ~ /Hgd> echo-e "123 // 456"

123 // 456

[Email protected]: ~ /Hgd> echo-e "123 // 456"

123/456

[Email protected]: ~ /Hgd & gt; echo-e "123/100456"

[Email protected]

 

Notes:

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 (!)

Example:

[Email protected]: ~> Echo "'test '"

-Bash: Test: Command not found

[Email protected]: ~> Echo ''test''

'Test'

[Email protected]: ~> Echo "$ test"

[Email protected]: ~> Echo '$ Test'

$ Test

[Email protected]: ~> Echo "// test"

/Test

[Email protected]: ~> Echo '// Test'

// Test

[Email protected]: ~> Echo "Hello! "

Echo "hello"

Hello

[Email protected]: ~> Echo 'Hello! '

Hello!

 

8. More, less, clear

N more, 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;

N clear command

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

 

9. head and tail

The n head 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 N tail-F command allows you to view new content in the file in real time.

 

10. 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:

[Email protected]: ~> WC 1.txt 2.txt

460 1679 16353 1.txt

300 1095 10665 2.txt

760 2774 27018 total usage

[Email protected]: ~> 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.

 

11. 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.

Example:

$ 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.

 

12. 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.

 

13. 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

 

Xargs:

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

 

Example:

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

 

 

14. 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

/Home/HJ

> Basename 1.txt

1. txt

> Dirname 1.txt

.

15. 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: info-contact@alibabacloud.com 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.