Linux Common Commands-redirect, Pipeline chapter (IV)

Source: Internet
Author: User

First, the Linux redirect

Redirection enables the input and output of Linux commands to redirect between files, and enables more powerful commands to combine multiple commands. Some of the more important commands that are involved in this section are:

The more important commands involved are:

    • Cat: Connection File
    • Sort: Sorting text lines
    • Uniq: Ignore or report duplicate rows
    • WC: Number of rows, words, and bytes of a statistic file
    • grep: Printing lines that match the development pattern
    • Head: The header of the output file
    • Tail: Tail of output file
    • Tee: Read from standard input and write to standard output or file

1. REDIRECT Standard output

Use > to redirect standard output information that would otherwise appear on the screen to a file.

Instead of overwriting the original content while implementing the redirect, >> appends the content to the end of the file.

2. REDIRECT standard error message to file

There is no simple operator to redirect standard error information to a file. There are two ways to achieve this. The first uses a file descriptor, in the shell, by default the digital 0,1,2 represents standard input, standard output, standard error, respectively.

3. Block the information you don't want to see

Linux has a special file/dev/null, which is called bit bucket, can accept input information but do nothing. Therefore, to suppress command output information, only the following actions are required:

  ls/usr/> /dev/null

4. REDIRECT Standard input

The cat command can accept a set of files as parameters and copy those files to standard output. The cat command has been used many times in our previous example to read the contents of a text file to the screen.

Cat also has a very important function: accept a set of files and then re-assemble them into a file by redirecting them . Let's look at an example:

  [email protected]:~$ catls1

  aaaaaaaaaaaaaaaaaaaaaaaa   [email protected]:~$ cat  ls2   bbbbbbbbbbbbbbbbbbbbbbbb   [email protected]:~$ cat  ls1 ls2 > ls   [email protected]:~$ cat  ls   aaaaaaaaaaaaaaaaaaaaaaaa   bbbbbbbbbbbbbbbbbbbbbbbb

Other examples:

Ls-l/usr/tmp >dir redirect

Ls-l/usr/tmp >>dir Chase aggravating direction

Ls/usr/tmp 2>err.file Error Output to Err.file

Ls/usr/tmp >output.file 2>&1 errors and outputs redirected to output

Ls/usr/tmp 2>&1 >output.file error to standard output, other input to output

WC </etc/passwd

The standard input, output, and error outputs are represented as stdin,stdout,stderr, or can be represented by 0,1,2

Second, the pipeline

write the standard output of a program to a file, and then use the file as input to another program. The solution to the pipeline is to combine two commands without the need for temporary files. such as:Ls/usr/bin | wc-w Linux provides the | operator, which can use the output of one command as input to another, so that more complex content is accomplished through the collaboration of multiple commands. For example, we can use the Uniq command to exclude duplicate rows. However, uniq commands often need to be used in conjunction with the sort command. Because Uniq receives a set of ordered text, just sort can sort the lines of text. Let's look at the following example:[[email protected]] #cat ls   aaaaaaaaaaaaaaaaaaaaaaaa   bbbbbbbbbbbbbbbbbbbbbbbb   aaaaaaaaaaaaaaaaaaaaaaaa   [[email protected]] #cat ls |sort|uniq   aaaaaaaaaaaaaaaaaaaaaaaa   bbbbbbbbbbbbbbbbbbbbbbbb

We can also add the-D option to the uniq command to see the duplicate rows, as follows:

[[email protected]] #cat ls |sort|uniq-d

Aaaaaaaaaaaaaaaaaaaaaaaa

Use the WC command to count the number of lines, Word books, and bytes of text. You can make a pipeline command with output such as the LS command:

[[Email ls /bin /usr/bin/ | sort protected]]# | uniq| wc

  

If you only want to count the number of rows, simply add the-l option after the WC command. The WC command is commonly used to count the number of lines of code.

[[Email find cn/ -name "*.php" | xargs protected]]# cat | wc-l

The above example counts the sum of the number of rows in the CN directory for all PHP files.

The grep command is used to find the matching pattern and to print the rows that contain the pattern. As an example:

  [[email protected]]#ls/bin /usr/bin/ | sort | uniq | grepzip

In this example, zip is a pattern that represents a zip containing a string. We found a program with a zip in the/bin and/usr/bin directories and printed the names of the programs. The grep command can use regular expressions, and the functionality is very powerful, and here we don't focus on the introduction.

The head and tail commands are also often used to compose pipeline commands. Head can view the previous part of a file or text. Tail View the end of a file or text. Both can receive the-n option, which indicates the number of rows displayed.

See Example:

  [[email protected]]#ls/bin/ | head-n 5

Another important use of tail is to display the log files of some programs in real time. Tail can accept an option-F, which shows the number of rows added to the end of the file in real time. Once a new row is appended to the end of the log file, it will be displayed in real time.

 [[email protected]]#tail -f /var/log/messages

Linux Common Commands-redirect, Pipeline chapter (IV)

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.