Ten good habits of operations in Linux (required by administrators)

Source: Internet
Author: User

Ten good habits to use are:

1. Create a directory tree in a single command.
2. Change the path. Do not move the archive.
3. Combine commands with control operators.
4. Exercise caution when referencing variables.
5. Use escape sequences to manage long input.
6. Group commands in the list.
7. Use xargs outside of find.
8. Know When grep should execute count -- When should bypass.
9. match some fields in the output, not just the rows.
10. Stop using pipelines for Cat.

Use 10 good habits that can improve your Unix command line efficiency-and get rid of bad usage patterns in this process. This article guides you step by step to learn several techniques used for command line operations, which are very good but are often ignored. Understand Common Errors and methods to overcome them so that you can understand exactly why these UNIX habits are worth using.
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.
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 a 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: using 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. IBM, Aix? Mkdir, GNU mkdir, and other systems that comply with the single UNIX specification currently have this option.

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 target file 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 place a semicolon (;) between commands on a single command line to combine 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. [P]
A command is run only when another command returns a non-zero exit status.
[P] 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 multiple lines, or your command line is smaller (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: using a 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: using a 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: run 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, ensure 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: run 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 examples

$ 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, this includes the following methods.
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. Output example generated by xargs

$ 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

~ /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: Using and without grep row counting

$ 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, and the WC MPs queue provides the total number of combinations for all files.

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, there 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 pattern 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 pattern 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 often 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.

Link: http://www.ibm.com/developerworks/cn/aix/library/au-badunixhabits.html

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.