This is a good habit that should be observed in the ten command line operation modes in linux. They not only make your operations faster, but also reduce the possibility of mistakes. They are
1. Use a mkdir statement to create a complete directory tree.
Example:
$ Mkdir-p project/{lib/ext, bin, src, doc/{html, info, pdf}, demo/stat/}
2. Use the-C option to specify the target folder during decompression.
Example:
$ Tar xvf-C tmp/a/B/c newarc.tar.gz
3. Use logical controllers to combine your operations
Example:
$ Cd tmp/a/B/c & tar xvf ~ /Archive.tar
4. Use double quotation marks to avoid misunderstanding.
Example:
$ Ls tmp/
A B
$ VAR = "tmp /*"
$ Echo $ VAR
Tmp/a tmp/B
$ Echo "$ VAR"
Tmp /*
5. Use the Esc character to input too long command lines
Example:
$ Cd tmp/a/B/c |/
> Mkdir-p tmp/a/B/c &&/
> Tar xvf-C tmp/a/B/c ~ /Archive.tar
6. Combine your operations into a list
Example:
$ (Cd tmp/a/B/c/| mkdir-p tmp/a/B/c &&/
> VAR = $ PWD; cd ~; Tar xvf-C $ VAR archive.tar )/
> | Mailx admin-S "Archive contents"
7. Use the xargs command to filter the output of the find command.
Example:
~ /Tmp $ ls-1 | xargs
December_report1_readme a archive.tar mkdirhier. sh
~ /Tmp $ ls-1 | xargs file
December_Report.pdf: PDF document, version 1.3
README: ASCII text
A: directory
Archive.tar: POSIX tar archive
Mkdirhier. sh: Bourne shell script text executable
~ /Tmp $
8. Know when to use grep to count, and when not
Example:
~ $ Time grep and tmp/a/longfile.txt | wc-l
2811
Real 0m0. 097 s
User 0m0. 006 s
Sys 0m0. 032 s
9. when determining whether a specific domain meets the conditions, use awk to replace grep
Example:
~ /Tmp $ ls-l | awk '$6 = "Dec "'
-Rw-r -- 3 joe 5096 Dec 14 archive.tar
-Rw-r -- 1 root 238 Dec 03 08:19 README
10. do not list the file content by using the cat command and pass it to grep. grep can directly search for the file.
Example:
~ $ Time grep and tmp/a/longfile.txt
2811
Real 0m0. 010 s
User 0m0. 006 s
Sys 0m0. 004 s
Reprinted statement:This article from http://jandan.net/2006/12/17/learn-10-good-unix-usage-habits.html
========================================================== ==========================================
Extension reference:
Ten habits of UNIX experts
Introduction
When you often use a system, it is often in a fixed usage mode. Sometimes, you do not get into the habit of doing things in the best way possible. Sometimes, your bad habits may even lead to confusion. One of the best ways to correct such defects is to consciously adopt the good habits of resisting these bad habits. This article presents 10 UNIX Command Line habits worth using-a good habit of helping you overcome many common usage quirks and improve the efficiency of command line work in this process. These 10 good habits are listed below, and a more detailed description is given later.
Use 10 good habits
Ten good habits to use are:
Create a directory tree in a single command.
Change the path. Do not move the archive.
Combines commands with control operators.
Exercise caution when referencing variables.
Use escape sequences to manage long input.
Group commands in the list.
Use xargs outside of find.
Know when grep should execute count-When should bypass.
Matches some fields in the output, not just the rows.
Stop using pipelines for cat.
Create a directory tree in a single command
Listing 1 demonstrates one of the most common UNIX bad habits: defining a directory tree at a time.
Listing 1. Bad habits 1 Example: define each directory tree separately
~ $ Mkdir tmp
~ $ Cd tmp
~ /Tmp $ mkdir
~ /Tmp $ cd
~ /Tmp/a $ mkdir B
~ /Tmp/a $ cd B
~ /Tmp/a/B/$ mkdir c
~ /Tmp/a/B/$ cd c
~ /Tmp/a/B/c $
Using the-p option of mkdir and creating all parent directories and Their subdirectories in a single command is much easier. However, even for administrators who know this option, they are still bound to gradually creating sub-directories at each level when creating sub-directories on the command line. It is worth taking the time to consciously develop this good habit:
Listing 2. Example of Good Habit 1: Use a command to define the directory tree ~ $ Mkdir-p tmp/a/B/c
You can use this option to create the entire complex directory tree (which is ideal for use in scripts), instead of simply creating a simple hierarchy. For example:
Listing 3. Another example of good habit 1: Use a command to define a complex directory tree ~ $ Mkdir-p project/{lib/ext, bin, src, doc/{html, info, pdf}, demo/stat/}
In the past, the only excuse for defining directories separately was that your mkdir implementation did not support this option, but it was no longer the case in most systems. This option is now available for IBM, AIX, mkdir, GNU mkdir, and other systems that comply with a Single UNIX Specification.
For a few systems that still lack this function, you can use the mkdirhier script (see references), which is the packaging of mkdir that executes the same function :~ $ Mkdirhier project/{lib/ext, bin, src, doc/{html, info, pdf}, demo/stat/}
Change path; Do not move the archive
Another bad usage mode is to move the. tar archive file to a directory, because this directory is exactly the directory where you want to extract the. tar file. In fact, you do not need to do this. You can decompress any. tar archive file to any directory-this is the purpose of the-C option. When extracting an archive file, use the-C option to specify the directory in which the file is to be decompressed:
Listing 4. Example of Good Habit 2: Use Option-C to decompress the. tar archive file ~ $ Tar xvf-C tmp/a/B/c newarc.tar.gz
In contrast to moving an archive file to the location where you want to decompress it, switch to this directory before decompressing it, the habit of using-C is more desirable-especially when the archive file is located somewhere else.
Combine commands with Control Operators
You may already know that in most shells, you can combine commands on a single command line by placing a semicolon (;) between commands. This Semicolon is a Shell control operator. Although it is useful for concatenating discrete commands on a single command line, it is not applicable to all situations. For example, if you use a semicolon to combine two commands, the correct execution of the second command is completely dependent on the successful completion of the first command. If the first command does not exit as expected, the second command will still run -- the result will cause failure. Instead, you should use more appropriate control operators (some operators described in this article ). As long as your Shell supports them, it is worth developing the habit of using them.
Run a command only when the other command returns the zero exit status.
Use the & control operator to combine two commands to run the second command only when the first command returns the zero exit status. In other words, if the first command runs successfully, the second command runs. If the first command fails, the second command does not run at all. For example:
Listing 5. Examples of good habits 3: combining commands with control operators ~ $ Cd tmp/a/B/c & tar xvf ~ /Archive.tar
In this example, the archived content is extracted ~ /Tmp/a/B/c directory, unless the directory does not exist. If the directory does not exist, the tar command will not run, so it will not extract any content.
A command is run only when another command returns a non-zero exit status.
Similarly, | the control operator separates two commands and runs the second command only when the first command returns a non-zero exit status. In other words, if the first command is successful, the second command will not run. If the first command fails, the second command will run. This operator is usually used to test whether a given directory exists. If the directory does not exist, create it:
Listing 6. Another example of good habits 3: combining commands with control operators ~ $ Cd tmp/a/B/c | mkdir-p tmp/a/B/c
You can also combine the control operators described in this section. Each operator affects the execution of the final command:
Listing 7. Examples of good habits 3: combining commands with control operators ~ $ Cd tmp/a/B/c | mkdir-p tmp/a/B/c & tar xvf-C tmp/a/B/c ~ /Archive.tar
Exercise caution when referencing variables
Always use Shell extensions and variable names with caution. Generally, it is best to include variable calls in double quotation marks unless you have enough reasons for not doing so. Similarly, if you use a variable name directly after a letter or number, make sure that the variable name is included in square brackets, to separate it from the surrounding region. Otherwise, Shell interprets the trailing text as part of the variable name -- and may return a null value. Listing 8 provides examples of variables for reference and non-reference and their impact.
Listing 8. Example of good habits 4: Referencing (and not referencing) variables ~ $ Ls tmp/
A B
~ $ VAR = "tmp /*"
~ $ Echo $ VAR
Tmp/a tmp/B
~ $ Echo "$ VAR"
Tmp /*
~ $ Echo $ VARa
~ $ Echo "$ VARa"
~ $ Echo "$ {VAR}"
Tmp/*
~ $ Echo $ {VAR}
Tmp/
~ $
Use escape sequences to manage long input
You may have seen a code example that uses a backslash (/) to extend a long line to the next line, in addition, you know that most shells treat the content you typed on subsequent rows connected by backslash as a single long line. However, you may not use this function in the command line as usual. If your terminal cannot properly process multi-line loopback, or your command line is smaller than usual (for example, when there is a long path in the prompt), The backslash is particularly useful. The backslash is also useful for understanding the meaning of long input lines, as shown in the following example:
Listing 9. Example of Good Habit 5: Use the backslash for long input ~ $ Cd tmp/a/B/c |/
> Mkdir-p tmp/a/B/c &&/
> Tar xvf-C tmp/a/B/c ~ /Archive.tar
Alternatively, you can use the following configurations:
Listing 10. alternative example of Good Habit 5: Use the backslash for long input ~ $ Cd tmp/a/B/c/
>||/
> Mkdir-p tmp/a/B/c/
> &&/
> Tar xvf-C tmp/a/B/c ~ /Archive.tar
However, when you divide an input row into multiple rows, Shell always treats it as a single continuous row because it always deletes all backslash and extra spaces.
Note: In most shells, when you press the up arrow, the entire multi-line input will be re-painted to a single long input line.
Group commands in the list
Most shells have methods to group commands in the list so that you can pass their aggregate output down to a pipe or redirect any part or all of the streams to the same place. Generally, you can run a command list in a Subshell or a command list in the current Shell.
Run Command list in Subshell
Use parentheses to include the command list in a single group. In this way, the command will be run in a new Subshell, And you can redirect or collect the output of the entire group of commands, as shown in the following example:
Listing 11. Example of good habits 6: running the command list in Subshell ~ $ (Cd tmp/a/B/c/| mkdir-p tmp/a/B/c &&/
> VAR = $ PWD; cd ~; Tar xvf-C $ VAR archive.tar )/
> | Mailx admin-S "Archive contents"
In this example, the archived content is extracted to the tmp/a/B/c/directory, and the output of the group command (including the list of extracted files) send the email to the admin address.
When you redefine the environment variables in the command list, and you do not want to apply those definitions to the current Shell, Subshell is preferred.
Run Command list in Current Shell
Enclose the command list with braces ({}) to run the command in the current Shell. Make sure that there are spaces between the brackets and the actual commands. Otherwise, Shell may not be able to correctly interpret the brackets. In addition, make sure that the last command in the list ends with a semicolon, as shown in the following example:
Listing 12. Another example of good habits 6: running the command list in the Current Shell ~ $ {Cp $ {VAR} a. & chown-R guest. guest &&/
> Tar cvf newarchive.tar a;} | mailx admin-S "New archive"
Use xargs outside find
Use the xargs tool as a filter to take full advantage of the output selected from the find command. The find operation usually provides a list of files that match certain conditions. This list is passed to xargs, which then uses this file list as a parameter to run some other useful commands, as shown in the following example:
Listing 13. Typical xargs usage example ~ $ Find some-file-criteria some-file-path |/
> Xargs some-great-command-that-needs-filename-arguments
However, do not regard xargs as a helper tool for finding; it is one of the underutilized tools, and when you get into the habit of using it, you will want to perform all the experiments, it includes the following usage.
List of passed space Separation
In the simplest form of calling, xargs is like a filter that accepts a list (each member is on a separate row) as input. This tool places those members on rows separated by a single space:
Listing 14. xargs output example ~ $ Xargs
A
B
C
Control-D
A B c
~ $
You can send the output of any tool that outputs file names through xargs to obtain the parameter list for some other tools that accept file names as parameters, as shown in the following example:
Listing 15. Example of xargs tool ~ /Tmp $ ls-1 | xargs
December_report1_readme a archive.tar mkdirhier. sh
~ /Tmp $ ls-1 | xargs file
December_Report.pdf: PDF document, version 1.3
README: ASCII text
A: directory
Archive.tar: POSIX tar archive
Mkdirhier. sh: Bourne shell script text executable
~ /Tmp $
The xargs command is not only used to pass file names. You can also use it whenever you need to filter text into a single row:
Listing 16. Example of Good Habit 7: Use xargs to filter text to a single row ~ /Tmp $ ls-l | xargs
-Rw-r -- 7 joe 12043 Jan 27 20:36 december_report1_- rw-r -- 1/
Root 238 Dec 03 README drwxr-xr-x 38 joe 354082 Nov 02/
A-rw-r -- 3 joe 5096 Dec 14 archive.tar-rwxr-xr-x 1/
Joe 3239 Sep 30 mkdirhier. sh
~ /Tmp $
Exercise caution when using xargs
Technically speaking, xargs is rarely troublesome. By default, the end string of a file is an underscore (_). If this character is sent as a single input parameter, all subsequent content is ignored. To prevent this, you can use the-e flag, which completely disables the end string without parameters.
Know when grep should execute count-when should it bypass
Avoid sending grep to wc-l in a pipeline to count the number of output rows. The-c option of grep provides a count of rows that match a specific pattern and is generally faster than sending data to wc through pipelines, as shown in the following example:
Listing 17. Example of good habit 8: use and do not use grep to count rows ~ $ Time grep and tmp/a/longfile.txt | wc-l
2811
Real 0m0. 097 s
User 0m0. 006 s
Sys 0m0. 032 s
~ $ Time grep-c and tmp/a/longfile.txt
2811
Real 0m0. 013 s
User 0m0. 006 s
Sys 0m0. 005 s
~ $
In addition to the speed factor, the-c option is a good way to execute the count. For multiple files, grep with the-c option returns a separate count for each file, with one count per row. For wc pipelines, the total number of combinations of all files is provided.
However, regardless of the speed, this example shows another common error to be avoided. These counting methods only provide the number of rows that contain the matching mode-if that is the result you are looking for, this is no problem. However, when a row has multiple instances in a specific mode, these methods cannot provide you with a real count of the number of instances actually matched. In the final analysis, you still need to use wc to count instances. First, run the grep command using the-o option (If your version supports it. This option only outputs the matching mode. Each line has one mode without losing the trip itself. However, you cannot use it with the-c option. Therefore, you must use wc-l to count rows, as shown in the following example:
Listing 18. Example of good habit 8: Using grep to count mode instances ~ $ Grep-o and tmp/a/longfile.txt | wc-l
3402
~ $
In this example, calling wc is a little faster than calling grep for the second time and inserting a virtual mode (such as grep-c) to match and count rows.
Matches some fields in the output, not just the rows.
When you only want to match the mode in a specific field in the output line, tools such as awk are superior to grep.
The following simplified example shows how to list only files modified on January 1, December.
Listing 19. Bad habits 9 example: Use grep to find the mode in a specific field ~ /Tmp $ ls-l/tmp/a/B/c | grep Dec
-Rw-r -- 7 joe 12043 Jan 27 December_Report.pdf
-Rw-r -- 1 root 238 Dec 03 08:19 README
-Rw-r -- 3 joe 5096 Dec 14 archive.tar
~ /Tmp $
In this example, grep filters rows and outputs all the files with Dec in the modification date and name. Therefore, files such as December_Report.pdf match, even if they have not been modified since January. This may not be the expected result. To match the pattern in a specific field, it is best to use awk. One of the Relational operators matches the exact field, as shown in the following example:
Listing 20. Examples of good habits 9: Use awk to find the mode in a specific field ~ /Tmp $ ls-l | awk '$6 = "Dec "'
-Rw-r -- 3 joe 5096 Dec 14 archive.tar
-Rw-r -- 1 root 238 Dec 03 08:19 README
~ /Tmp $
For more information about how to use awk, see references.
Stop using a cat Pipeline
A common basic usage error of grep is that cat output is sent to grep in a pipeline to search for the content of a single file. This is absolutely unnecessary. It is a waste of time, because tools such as grep accept file names as parameters. You do not need to use cat in this case, as shown in the following example:
Listing 21. Examples of good habits and bad habits 10: Use grep with or without cat
~ $ Time cat tmp/a/longfile.txt | grep and
2811
Real 0m0. 015 s
User 0m0. 003 s
Sys 0m0. 013 s
~ $ Time grep and tmp/a/longfile.txt
2811
Real 0m0. 010 s
User 0m0. 006 s
Sys 0m0. 004 s
~ $
This error exists in many tools. Since most tools accept standard input using hyphens (-) as a parameter, the parameter is usually invalid even if cat is used to distribute multiple files in stdin. Only when you use cat with one of multiple filtering options, it is really necessary to perform a connection before the pipeline.
Conclusion: good habits
It is best to check any bad usage mode in your command line habits. Poor usage modes may speed up and lead to unexpected errors. This article introduces 10 new habits that can help you get rid of many of the most common usage errors. Developing these habits is a positive step to enhance your UNIX Command Line skills.
Reprinted statement:This article from http://hi.baidu.com/zkheartboy/blog/item/fa1797cb65ff71fd52664f96.html