Ten habits of UNIX experts

Source: Internet
Author: User

Use 10 to improve your UNIX
Good Habit of 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. Learn more
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 proposes 10 Unix
Command Line habits-helps 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:

  1. Create a directory tree in a single command.
  2. Change the path. Do not move the archive.
  3. Combines 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. Infind
    External Usexargs
    .
  8. Know whengrep
    Count should be executed-When should I bypass.
  9. Matches some fields in the output, not just the rows.
  10. Stopcat
    Use pipelines.

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 a
~/tmp $ cd a
~/tmp/a $ mkdir b
~/tmp/a $ cd b
~/tmp/a/b/ $ mkdir c
~/tmp/a/b/ $ cd c
~/tmp/a/b/c $

Usemkdir
Of-p
It is much easier to create all parent directories and Their subdirectories in a single command. 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/a}

In the past, the only excuse for defining directories separately was yours.mkdir
The implementation does not support this option, but it is no longer the case in most systems. IBM, Aix,mkdir
, GNUmkdir
This option is now available for other systems that comply with the single UNIX specification.

You can usemkdirhier
Script (see references), which executes the same functionmkdir
Packaging:

~ $ mkdirhier project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}

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 as you like-this is-C
Option. When extracting an archive file, use-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

Instead of moving an archive file to the location where you want to decompress it, switch to the directory, and then decompress it.-C
This is especially true 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 ShellControl Operators
,
Although it is useful for concatenating discrete commands on a single command line, it does not apply to all situations. For example, if you use a semicolon to combine two commands, the correct execution of the second command is completely dependent
The first command is successfully completed. If the first command does not exit as expected, the second command will still run -- the result will cause failure. Instead, you should use a more appropriate control operator (this article will describe
Some operators ). 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&&
Control Operators to combine two commands to facilitateOnly when
The first command returns the zero exit status before running the second command. 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 existtar
The command does not run, so it does 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 state. In other words, if the first commandSuccessful
The second command does not run. If the first command fails, the second commandYes
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 with caution
Extension and variable name. 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
The volume name is included in square brackets ([]) to separate it from the surrounding partition. Otherwise, Shell
The trailing text is interpreted as part of the variable name -- and it is likely to 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}a"
tmp/*a
~ $ echo ${VAR}a
tmp/a
~ $

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, and you know that most Shells
The content you typed on subsequent rows connected by backslash is considered as a single long row. However, you may not use this function in the command line as usual. If your terminal cannot properly process multiple lines, or your
The command line is usually 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 is repainted 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 archive content is extracted to the tmp/A/B/C/directory, and the output of the group command (including the list of extracted files) is sent to the address by mail.admin
.

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 in braces ({})Current
Run in 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: run the command list in the Current Shell

~ $ { cp ${VAR}a . && chown -R guest.guest a && /
> tar cvf newarchive.tar a; } | mailx admin -S "New archive"

Use xargs outside find

Usexargs
Tool as a filter to make full usefind
Command selection output.find
Running usually provides a list of files that match certain conditions. This list is passedxargs
The latter then uses the 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 notxargs
Onlyfind
Is one of the underutilized tools. When you get into the habit of using it, you will want to perform all the experiments, including the following usage.

List of passed space Separation

In the simplest form of calling,xargs
Like a filter, it 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 sendxargs
To output the output of any tool for the file name, so as to obtain the parameter list for some other tools that accept the file name as a parameter, as shown in the following example:

Listing 15. Example of xargs

~/tmp $ ls -1 | xargs
December_Report.pdf 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 $

xargs
The command is not only used to pass the file name. 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--r-- 7 joe joe 12043 Jan 27 20:36 December_Report.pdf -rw-r--r-- 1 /
root root 238 Dec 03 08:19 README drwxr-xr-x 38 joe joe 354082 Nov 02 /
16:07 a -rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar -rwxr-xr-x 1 /
joe joe 3239 Sep 30 12:40 mkdirhier.sh
~/tmp $

Exercise caution when using xargs

Technically speaking, usexargs
There is little trouble. 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 situation, you can use-e
It completely disables the end string without parameters.

Know when grep should execute count-when should it bypass

Avoid addinggrep
Sendwc -l
To count the number of output rows.grep
Of-c
This option provides a count for the rows that match a specific pattern, and is generally compared towc
Faster, 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.097s
user 0m0.006s
sys 0m0.032s
~ $ time grep -c and tmp/a/longfile.txt
2811

real 0m0.013s
user 0m0.006s
sys 0m0.005s
~ $

In addition to the speed factor,-c
Option is a good way to execute the count. For multiple files-c
Optionalgrep
Returns the individual count of each file, one count per line, andwc
Provides the total number of combinations of all files.

However, regardless of the speed, this example shows another common error to be avoided. These counting methods only provideNumber of rows that contain the matching mode
-- If it is the result you want to search for, there is no problem. However, when a row has multiple instances in a specific mode, these methods cannot provide you with actual matchingInstance quantity
. In the final analysis, to count instances, you still need to usewc
To count. First, use-o
Option (If your version supports it) to rungrep
Command. This optionOnly
Output the matching mode. Each line has one mode without losing the trip itself. However, you cannot-c
You must usewc -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 examplewc
Compared with the second callgrep
Insert a virtual mode (for examplegrep -c
.

Matches some fields in the output, not just the rows.

If you only want to matchSpecific fields
In, suchawk
And other tools are bettergrep
.

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--r-- 7 joe joe 12043 Jan 27 20:36 December_Report.pdf
-rw-r--r-- 1 root root 238 Dec 03 08:19 README
-rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar
~/tmp $

In this example,grep
Filters rows and outputs the modified Date and nameDec
All files. 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 useawk
, A relational operator 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--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar
-rw-r--r-- 1 root root 238 Dec 03 08:19 README
~/tmp $

How to Useawk
For more information, see references.

Stop using a cat Pipeline

grep
A common basic usage error of iscat
Outputgrep
To search for the content of a single file. This is absolutely unnecessary. It is a waste of time, becausegrep
Such a tool accepts the file name as a parameter. You do not need to usecat
, 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.015s
user 0m0.003s
sys 0m0.013s
~ $ time grep and tmp/a/longfile.txt
2811

real 0m0.010s
user 0m0.006s
sys 0m0.004s
~ $

This error exists in many tools. Most tools accept standard input with hyphens (-) as a parameter, even ifcat
To distributestdin
And the parameters are usually invalid. Only when you use one of the multiple filtering optionscat
It is really necessary to execute the 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.

 

 Original article address

Http://linux.chinaunix.net/docs/2007-03-08/3956.shtml

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.