Linux ' | ' vs. redirected instances

Source: Internet
Author: User
Tags gopher

Redirection and Piping

---------- Small Command big as

REDIRECT standard input out directed command

The most used is to write content to a file

[Email protected] ~]# echo "Hello,world" >1.txt

[email protected] ~]# cat 1.txt

Hello,world

Redirect the results of a command to a file

[Email protected] ~]# cat/etc/passwd >2.txt

[email protected] ~]# cat 2.txt

Root:x:0:0:root:/root:/bin/bash

Bin:x:1:1:bin:/bin:/sbin/nologin

Daemon:x:2:2:daemon:/sbin:/sbin/nologin

Adm:x:3:4:adm:/var/adm:/sbin/nologin

Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Sync:x:5:0:sync:/sbin:/bin/sync

Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Halt:x:7:0:halt:/sbin:/sbin/halt

Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

Operator:x:11:0:operator:/root:/sbin/nologin

Games:x:12:100:games:/usr/games:/sbin/nologin

Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin

Nobody:x:99:99:nobody:/:/sbin/nologin

Dbus:x:81:81:system message Bus:/:/sbin/nologin

Usbmuxd:x:113:113:usbmuxd User:/:/sbin/nologin

Vcsa:x:69:69:virtual Console Memory Owner:/dev:/sbin/nologin

Rtkit:x:499:497:realtimekit:/proc:/sbin/nologin

Avahi-autoipd:x:170:170:avahi Ipv4ll Stack:/var/lib/avahi-autoipd:/sbin/nologin

Abrt:x:173:173::/etc/abrt:/sbin/nologin

Haldaemon:x:68:68:hal Daemon:/:/sbin/nologin

Gdm:x:42:42::/var/lib/gdm:/sbin/nologin

>>: append standard output REDIRECT

[Email protected] ~]# echo " Wang Pengjin,seniorJava engineer " >> 1.txt

[email protected] ~]# cat 1.txt

Hello,world

Wang Pengjin, SeniorJava engineer

If you do not have to append redirects, the original contents of the file will be replaced, as

[Email protected] ~]# echo " Majiang, in Beijing " >1.txt

[email protected] ~]# cat 1.txt

Majiang, in Beijing

[Email protected] ~]#

2>: redirect error standard output

If there is an error in the command, the command is executed and the error message is output in the text, such as:

[Email protected] ~]# Ls/etc/pas 2> 1.txt

[email protected] ~]# cat 1.txt

Ls:cannot access/etc/pas:no such file or directory

[Email protected] ~]#

2>>: Append redirect error standard output

Append the command error message to the text

[Email protected] ~]# echo " Majiang, in Beijing " >1.txt

[Email protected] ~]# Ls/etc/pas 2>>1.txt

[email protected] ~]# cat 1.txt

Majiang, in Beijing

Ls:cannot access/etc/pas:no such file or directory

&>: redirect standard output or errors to the same folder

Error output

[Email protected] ~]# ls/var2 &> 1.txt

[email protected] ~]# cat 1.txt

Ls:cannot access/var2:no such file or directory

[Email protected] ~]#

Correct output

[Email protected] ~]# Ls/var &> 1.txt

[email protected] ~]# cat 1.txt

Account

Cache

Crash

Cvs

Db

Empty

Games

Gdm

Lib

Local

Lock

Log

Mail

Nis

Opt

Preserve

Run

Spool

Tmp

Www

Yp

[Email protected] ~]#

<: input redirection

You can use the result as another command, such as

convert everything inside the /etc/passwd file to uppercase

Tr ' A-Z ' A-Z ' </etc/passwd

[[Email protected] ~]# TR ' A-Z ' A-Z ' </etc/passwd

Root:x:0:0:root:/root:/bin/bash

Bin:x:1:1:bin:/bin:/sbin/nologin

Daemon:x:2:2:daemon:/sbin:/sbin/nologin

Adm:x:3:4:adm:/var/adm:/sbin/nologin

Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Sync:x:5:0:sync:/sbin:/bin/sync

Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Halt:x:7:0:halt:/sbin:/sbin/halt

Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

Operator:x:11:0:operator:/root:/sbin/nologin

Games:x:12:100:games:/usr/games:/sbin/nologin

Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin

Nobody:x:99:99:nobody:/:/sbin/nologin

Dbus:x:81:81:system MESSAGE Bus:/:/sbin/nologin

Usbmuxd:x:113:113:usbmuxd User:/:/sbin/nologin

Vcsa:x:69:69:virtual CONSOLE MEMORY Owner:/dev:/sbin/nologin

Rtkit:x:499:497:realtimekit:/proc:/sbin/nologin

Avahi-autoipd:x:170:170:avahi Ipv4ll Stack:/var/lib/avahi-autoipd:/sbin/nologin

Abrt:x:173:173::/etc/abrt:/sbin/nologin

Haldaemon:x:68:68:hal Daemon:/:/sbin/nologin

Gdm:x:42:42::/var/lib/gdm:/sbin/nologin

<<: generates a document in the current directory, such as

[[email protected] ~]# cat >> 1.txt << End

> Majiang in Beijing, Wang Pengjin is a senior JAVA engineer

> Joe IV is a senior hardware engineer

> End

[email protected] ~]# cat 1.txt

Majiang in Beijing, Wang Pengjin is a senior JAVA engineer

Joe IV is a senior hardware engineer.

'| ' pipe symbol, you can use the result of the previous command output as input to the following command

Command 1| Command 2| Command 3|

Example: Shows the first bit in the/eyc/passwd file

CAT/ETC/PASSWD |cut-d:-f1

Root

Bin

Daemon

Adm

Lp

Sync

Shutdown

Halt

Mail

Uucp

operator

Games

Gopher

Ftp

Nobody

Dbus

Usbmuxd

Vcsa

Rtkit

Avahi-autoipd

Abrt

Haldaemon

And sort the returned results in alphabetical order

[Email protected] ~]# cat/etc/passwd |cut-d:-f1| Sort-n

Abrt

cd/

Apache

Avahi-autoipd

Bin

Daemon

Dbus

Ftp

Games

Gdm

Gopher

Haldaemon

Halt

Lp

Mail

Mailnull

Mysql

Nobody

Ntp

operator

Postfix

Pulse

Root

Rtkit

Saslauth

Shutdown

Smmsp

Sshd

Sync

Tcpdump

Usbmuxd

Uucp

Vcsa

Www

The output is then shown in uppercase letters:

[Email protected] ~]# cat/etc/passwd |cut-d:-f1| sort-n| Tr ' A-Z ' A-Z

ABRT

Adm

Apache

Avahi-autoipd

BIN

DAEMON

DBUS

Ftp

Games

Gdm

GOPHER

Haldaemon

HALT

Lp

MAIL

Mailnull

Mysql

NOBODY

Ntp

OPERATOR

POSTFIX

PULSE

ROOT

Rtkit

Saslauth

SHUTDOWN

Smmsp

Sshd

SYNC

TCPDUMP

Usbmuxd

UUCP

VCSA

Www

[Email protected] ~]#

Gets the line in the/etc/passwd file

[Email protected] ~]# head-10/etc/passwd |tail-1

Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

Gets the line 8 in the/etc/passwd file ,

Abrt:x:173:173::/etc/abrt:/sbin/nologin

Haldaemon:x:68:68:hal Daemon:/:/sbin/nologin

Gdm:x:42:42::/var/lib/gdm:/sbin/nologin

Ntp:x:38:38::/etc/ntp:/sbin/nologin

Apache:x:48:48:apache:/var/www:/sbin/nologin

SASLAUTH:X:498:76:SASLAUTHD User:/var/empty/saslauth:/sbin/nologin

Postfix:x:89:89::/var/spool/postfix:/sbin/nologin

Pulse:x:497:496:pulseaudio System Daemon:/var/run/pulse:/sbin/nologin

Sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologin

Tcpdump:x:72:72::/:/sbin/nologin

Mysql:x:500:500:mysq:/home/mysql:/bin/bash

Www:x:501:501::/home/www:/bin/false

Mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

Smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin

[Email protected] ~]# tail-8/etc/passwd |head-1

Postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[Email protected] ~]#

Linux ' | ' vs. redirected instances

Related Article

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.