Linux Data flow redirection and piping

Source: Internet
Author: User
Tags bz2

Data flow redirection simply means that the data that should have been exported somewhere (such as a screen) is redirected to its output destination, to other places (such as files).

Input and output in Linux:

Standard input (stdin): Default input from keyboard

Standard output (stdout): Normal result information performed, default output to screen

Standard error Output (STDERR): Error message executed, default output to screen

Then let's experience these three ghosts:

[[email protected] 08:32 ~]$ ll <-- standard output, default result output to total screen usage24drwxrwxr-x.2 FUWH fuwh 4096 July 30 13:42bindrwxr-xr-x.3 FUWH fuwh 4096 July 26 12:04datadrwxrwxr-x.2 FUWH fuwh 4096 August 3 20:34studrwxrwxr-x.3 FUWH fuwh 4096 July 25 10:27stu2-rw-r--r--.1 root root 2439 July 26 11:57stu2.tar.bz2-rw-r--r--.1 root root 165 July 26 11:57stu2.tar.gz[[email protected] 08:33 ~]$ ll dat <-- standard error output, default result output to screen ls: Unable to access dat: No file or directory[[email protected] 08:33 ~]$ cat namecat:name: No file or directory[[email protected] 08:33 ~]$ cat hecat:he: No file or directory[[email protected] 08:33 ~]$ cat > He <-- standard input, default keyboard Namego[[email protected] 08:34 ~]$

Output or Error output redirection Yes, I don't want to output this information to the screen, I want to output it in a file.

Input from orientation is I don't want to input from the keyboard, I want to use a file as input and so on.

Standard input: Code 0, using < or <<

Standard output: Code 1, using > or >>

Standard error Output: Code 2, using 2> or 2>>

※>> or << means accumulation, where > means 1>

Let's try it out here:

[[email protected] 08:41 ~/stu]$ ll Total dosage0[[email protected] 08:41 ~/stu]$ LL.. /Total Dosage28drwxrwxr-x.2 FUWH fuwh 4096 July 30 13:42bindrwxr-xr-x.3 FUWH fuwh 4096 July 26 12:04data-rw-rw-r--.1 FUWH fuwh 8 August 5 08:33hedrwxrwxr-x.2 FUWH fuwh 4096 August 5 08:41studrwxrwxr-x.3 FUWH fuwh 4096 July 25 10:27stu2-rw-r--r--.1 root root 2439 July 26 11:57stu2.tar.bz2-rw-r--r--.1 root root 165 July 26 11:57stu2.tar.gz[[email protected] 08:42 ~/stu]$ LL.. /> Home.txt <-- Standard output Redirect, redirect to a file [[email protected] 08:42 ~/stu]$ LL.. /DATLS: Cannot access. /dat: No file or directory[[email protected] 08:42 ~/stu]$ LL.. /dat 2> Err.txt <-- standard error output redirection redirect to a file [[email protected] 08:42 ~/stu]$[[email protected] 08:42 ~/stu]$ cat > C.txtname[[email protected] 08:44 ~/stu]$ cat C.txtnameage[[email protected] 08:44 ~/stu]$ cat home.txt Total usage28drwxrwxr-x.2 FUWH fuwh 4096 July 30 13:42bindrwxr-xr-x.3 FUWH fuwh 4096 July 26 12:04data-rw-rw-r--.1 FUWH fuwh 8 August 5 08:33hedrwxrwxr-x.2 FUWH fuwh 4096 August 5 08:42studrwxrwxr-x.3 FUWH fuwh 4096 July 25 10:27stu2-rw-r--r--.1 root root 2439 July 26 11:57stu2.tar.bz2-rw-r--r--.1 root root 165 July 26 11:57stu2.tar.gz[[email protected] 08:44 ~/stu]$ cat > C.txt < home.txt <-- standard input redirection, from file input [[email protected] 08:44 ~/stu]$ cat c.txt Total usage28drwxrwxr-x.2 FUWH fuwh 4096 July 30 13:42bindrwxr-xr-x.3 FUWH fuwh 4096 July 26 12:04data-rw-rw-r--.1 FUWH fuwh 8 August 5 08:33hedrwxrwxr-x.2 FUWH fuwh 4096 August 5 08:42studrwxrwxr-x.3 FUWH fuwh 4096 July 25 10:27stu2-rw-r--r--.1 root root 2439 July 26 11:57stu2.tar.bz2-rw-r--r--.1 root root 165 July 26 11:57stu2.tar.gz[[email protected] 08:44 ~/stu]$

What if we want both standard input and standard error input to be output to a file?

That's when you need to use a special notation.

[[email protected] 08:49 ~/stu]$ cat c.txt a.txt Total usage28drwxrwxr-x.2 FUWH fuwh 4096 July 30 13:42bindrwxr-xr-x.3 FUWH fuwh 4096 July 26 12:04data-rw-rw-r--.1 FUWH fuwh 8 August 5 08:33hedrwxrwxr-x.2 FUWH fuwh 4096 August 5 08:42studrwxrwxr-x.3 FUWH fuwh 4096 July 25 10:27stu2-rw-r--r--.1 root root 2439 July 26 11:57stu2.tar.bz2-rw-r--r--.1 root root 165 July 26 11:57stu2.tar.gzcat:a.txt: No file or directory[[email protected] 08:49 ~/stu]$cat c.txt a.txt > Jt.txt 2>&1[[email protected] 08:50 ~/stu]$ cat jt.txt Total usage28drwxrwxr-x.2 FUWH fuwh 4096 July 30 13:42bindrwxr-xr-x.3 FUWH fuwh 4096 July 26 12:04data-rw-rw-r--.1 FUWH fuwh 8 August 5 08:33hedrwxrwxr-x.2 FUWH fuwh 4096 August 5 08:42studrwxrwxr-x.3 FUWH fuwh 4096 July 25 10:27stu2-rw-r--r--.1 root root 2439 July 26 11:57stu2.tar.bz2-rw-r--r--.1 root root 165 July 26 11:57stu2.tar.gzcat:a.txt: No file or directory[[email protected] 08:50 ~/stu]$ Cat c.txt a.txt &> jt2.txt [[email protected] 08:51 ~/stu]$ cat jt2.txt Total usage28drwxrwxr-x.2 FUWH fuwh 4096 July 30 13:42bindrwxr-xr-x.3 FUWH fuwh 4096 July 26 12:04data-rw-rw-r--.1 FUWH fuwh 8 August 5 08:33hedrwxrwxr-x.2 FUWH fuwh 4096 August 5 08:42studrwxrwxr-x.3 FUWH fuwh 4096 July 25 10:27stu2-rw-r--r--.1 root root 2439 July 26 11:57stu2.tar.bz2-rw-r--r--.1 root root 165 July 26 11:57stu2.tar.gzcat:a.txt: No file or directory[[email protected] 08:51 ~/stu]$

But, imagine, if sometimes the error message, we do not want to output to the screen, and do not want to save to the file, what should be done?

At this point, you need to use a device for a black hole in a trash can.

[[email protected] 08:53 ~/stu]$ ll Total dosage20-rw-rw-r--.1 FUWH fuwh 367 August 5 08:44c.txt-rw-rw-r--.1 FUWH FUWH 52 August 5 08:42err.txt-rw-rw-r--.1 FUWH fuwh 367 August 5 08:42home.txt-rw-rw-r--.1 FUWH FUWH 407 August 5 08:51jt2.txt-rw-rw-r--.1 FUWH FUWH 407 August 5 08:50Jt.txt[[email protected] 08:54 ~/stu]$ ll > /dev/null [[email protected] 08:54 ~/stu]$ ll Total dosage20-rw-rw-r--.1 FUWH fuwh 367 August 5 08:44c.txt-rw-rw-r--.1 FUWH FUWH 52 August 5 08:42err.txt-rw-rw-r--.1 FUWH fuwh 367 August 5 08:42home.txt-rw-rw-r--.1 FUWH FUWH 407 August 5 08:51jt2.txt-rw-rw-r--.1 FUWH FUWH 407 August 5 08:50Jt.txt[[email protected] 08:54 ~/stu]$

Pipeline: |

The pipe command is the standard output of the previous command as the normal input for the next command

[[email protected] 08:57 ~/stu]$ ll Total dosage0[[email protected] 08:57 ~/stu]$ touch A.txt[[email protected] 08:57 ~/stu]$ touch Ab.txt[[email protected] 08:57 ~/stu]$ ll Total dosage0-rw-rw-r--.1 fuwh fuwh 0 August 5 08:57ab.txt-rw-rw-r--.1 fuwh fuwh 0 August 5 08:57a.txt[[email protected] 08:57 ~/stu]$ ll | cat > C.txt[[email protected] 08:57 ~/stu]$ ll Total dosage4-rw-rw-r--.1 fuwh fuwh 0 August 5 08:57ab.txt-rw-rw-r--.1 fuwh fuwh 0 August 5 08:57a.txt-rw-rw-r--.1 FUWH FUWH 154 August 5 08:57C.txt[[email protected] 08:58 ~/stu]$ cat c.txt Total usage0-rw-rw-r--.1 fuwh fuwh 0 August 5 08:57ab.txt-rw-rw-r--.1 fuwh fuwh 0 August 5 08:57a.txt-rw-rw-r--.1 fuwh fuwh 0 August 5 08:57C.txt[[email protected] 08:58 ~/stu]$
[[email protected] 08:59 ~/stu] $ ll A.txt d.txt | cat > F.TXTLS: Unable to access d.txt: No file or directory

It can be found that the standard error output is not routed by the pipeline at this time.

Linux Data flow redirection and piping

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.