Linux Redirection and Plumbing tutorials

Source: Internet
Author: User

1.I/O Redirect: input, output, let's take a look at those inputs that are output:

the input data stream:<----standard input (stdin) keyboard;

Output Data flow:----> Standard output (stdout) display;

Incorrect output stream:----> Error output (stderr) display;

2. In the shell, the standard input and the standard output, as well as the error output, each have a code name.

Standard Input: 0

Standard output: 1

Error Output: 2

3. Redirection

Read-in from standard input, standard output output, exception report to standard error.

change standard output with >

Command > file redirects the standard output of the command to a file instead of printing it on the console.

[Email protected] nzg]# cat/etc/passwd >/HOME/NZG/A1

Change standard input with <

Command < file modifies the command's standard input to file

[Email protected] nzg]# Cat/dev/null </etc/passwd

Append files to >>

Command >> file appends the command output to the end

[Email protected] nzg]# cat/etc/passwd >>/home/nzg/a1

#管道的数据共享在Linux内核中是通过内存复制实现的, the movement of data tends to be more time consuming than the CPU, so when designing a pipeline, try to place the operations that can reduce the amount of data into the front of the pipeline. The data is copied quickly, and the program computation is reduced.

4. The Magical Magic of special documents

/dev/null a black hole, devouring all files, all files written to it are unreadable and useful for scripting.

[Email protected] nzg]# Cat/dev/null </etc/passwd

/dev/zero is used to create a file of a specified length and is initialized to be empty this file is generally used as a temporary interchange file

/dev/tty force information output to the specified terminal

[Email protected] nzg]# echo hahahaha111 >/DEV/PTS/1

5. Disable overwrite output redirection to existing files;

[Email protected] nzg]# set-c

You can use the force overwrite output at this time; >|

Turn on overwrite output redirect to existing file;

[[Email protected] nzg]# set +c

Error output stream redirection: 2>,2>>

[Email protected] nzg]# llls A1 2> 1.txt
[Email protected] nzg]# llls A1 22> 1.txt

Merge normal output stream and error output stream;

(1) &>,&>> (> Overlay,>> for append)

[Email protected] nzg]# llls A1 &> 1.txt
[Email protected] nzg]# llls A1 &>> 1.txt

(2) Commad >/path/to/somefile 2>&1

[Email protected] nzg]# llls al > 1.txt 2>&1

Commad >>/path/to/somefile 2>&1

[Email protected] nzg]# llls al >> 1.txt 2>&1

6. Piping

The combination of redirect and pipeline can play his greatest advantage, it will be very flexible and practical.

When using a pipe segment, we can imagine that two water pipes flow into the water pipe at the input of another program, which allows us to stitch the program arbitrarily.

Pipeline: A connection program that implements the output of the previous command directly after a program as an input data stream

COMMAND1 | COMMAND2 | COMMAND3 | ....

[Email protected] nzg]# HEAD-N10/ETC/PASSWD | TR A-Z

Just like this, we read the/etc/passwd file first, and then we're going to convert the lowercase to uppercase in the execution.

This article is from the "Linux Learning path" blog, so be sure to keep this source http://luyubo.blog.51cto.com/7634254/1833119

Linux Redirection and plumbing tutorial

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.