$ ls -F /etc | grep '/$' ← list directories but not files (common) $ ls -F /etc | grep -v '/$' | less ← list files but not directories Pipe to less $ cat file | more ← output file to flip reader $ echo abcd | tr [:lower:] [:upper:] ← output pipe to tr to uppercase output |
$ echo -n 'Linux' | od -An -tx1 ← List the ASCII code of the string "Linux" in hex. 4c 69 6e 75 78 |
$ cat /etc/passwd | grep '/home' | cut -d: -f1 | sort aaa bbb john austin |
Input | Command | Output | ||
fd 0,stdin (standard input) | → | programs | → | fd 1,stdout (standard output) |
→ | fd 2,stderr (standard error) |
symbol | action |
1> or > | STDOUT, because the File Descriptor of stdout is originally 1, so "1" can be omitted |
1>>or >> | STDOUT append redirection |
2> | error output (stderr) redirection |
2>> | STDERR append redirection |
0< 或 < | STDIN redirections, because the fd stdin is originally 0, so "0" can be omitted |
- | standard input |
2>&1 | stderr redirects stdout |
1>&2 或 >&2 | stdout redirects stderr |
<< delimmiter | end of input string |
$ echo 'Hello World' 1> redir.txt ← Output the original to the screen to the file "redir.txt" $ cat redir.txt ← Verify the file just generated by redirection (if this file does not exist originally, it will be created this file) Hello World $ echo '123' > redir.txt ← Overwrite the old file "redir.txt" (">"="1>" is to omit the fd Number) $ cat redir.txt ← Verify "redir.txt "Whether it is covered or not 123 |
$ echo '123' > redir1.txt $ echo '456' > redir2.txt $ cat redir1.txt redir2.txt > redir3.txt $ cat redir3.txt 123 456 |
$ cat > redir.txt↵ Enter ← read the data from the keyboard and redirect to the file "redir.txt" Linux is a PC OS↵ Enter ← can input any text which is based on free and open source .↵ Enter Ctrl+D ←Press <Ctrl+D> to end the file $ cat redir.txt ←verify that Linux is a PC OS which is based on free and open source . |
$ echo '123' 1>> acc_redir.txt ← cumulative redirection, if there is no "acc_redir.txt", this file will be created $ echo '456' >> acc_redir.txt ← If the original file already exists, the old file $ echo '789' >> acc_redir.txt $ cat acc_redir.txt ←Verify the cumulative redirected file 123 456 789 |
$ ls -R /home ←Log in as non-"root" and list the files in the directory /home one by one. (The intermediate output omits) ls: cannot open directory /home/bbb: Permission denied ←Insufficient permissions, unable to peek into others Home directory (this is the stderr part) $ ls -R /home 2> stderr.txt ←Redirect the stderr part to the file "stderr.txt" (that is, the stdout of the correct part is output to the screen, but the stderr part is no longer output to screen, but output to the file "stderr.txt") $ cat stderr.txt ←Verify the file "stderr.txt" ls: cannot open directory /home/bbb: Permission denied |
$ ls -R /home > ok.txt 2> err.txt ← output stdout to "ok.txt", stderr to "err.txt" (because both are output to the file, so the screen will not have any output |
$ ls -R /home 2> /dev/null ←The screen only displays the stdout, and the incorrect error output stderr is not displayed, just throw $ ls -R /home 1> /dev/null ←Contrary to the above example, the screen only displays the error part $ find / -name 'readme.*' 2> /dev/null ←Find the file called "readme" in the HD /usr/share/doc/words-3.0/readme.txt /usr/share/doc/lua-5.1.4/readme.html /usr/share/doc/marisa-0.2.4/readme.en.html |
$ echo 'abcd' > lower.txt ← Generate a file "lower.txt", the file content is "abcd" $ tr -d 'c' < lower.txt ←Use the tr filter program to filter the characters in the input file "c"filter out abd ←"c"is missing $ tr a-z A-Z <lower.txt> upper.txt ←The result of input redirection can also be redirected to the file $ cat < upper.txt ←use cat and redirection to read Fetch file "upper.txt" ABCD |
$ tr a-z A-Z <<< 'abc' ← Use tr to convert the lowercase string to uppercase ABC |
$ echo '123' > redir.txt ← Generate a file "redir.txt", the file content is "123" $ cat >> redir.txt - ← use the standard input "-" (keyboard input) to append & redirect to the file "redir.txt" 456↵ Enter ← Enter "456" from the keyboard and press <Enter> 789↵ Enter ← Enter "789" from the keyboard and press <Enter> Ctrl+D← press <Ctrl+D> end of file $ cat redir.txt ←The following is the sorted output 123 456 789 $ sort - ←Input two numbers and add z↵ Enter ←Input "z" on the keyboard and press <Enter> a↵ Enter ←Input "a" on the keyboard and press <Enter> Ctrl+D ←press <Ctrl+D> end of file a ←The following is the sorted output z $ awk '{print $1+$2}' - ←Input two numbers and add 1.23 4.56789 ←Enter any two numbers 5.7978 ←Output the result of adding two numbers (press <Ctrl-D > to end) |
$ echo "wxyZ" | cat - ← cat The content of the file to be listed comes from STDIN & the STDIN comes from the pipes wyyz $ cat file3 | cat file1 - file2 ← In this example, the output file order is file1, and the standard input (file3) comes again for file2 $ cat file2 | diff - file1 ← use the command diff to compare "file1" and "file2" |
$ ls -l /etc/cron.d /etc/fstab 2>&1 | tr a-z A-Z ←Pipeline stdout & stderr to tr and capitalize -RW-R--R-- 1 ROOT ROOT 608 2014-09-26 15:47 /ETC/FSTAB LS: CANNOT OPEN DIRECTORY /ETC/CRON.D: PERMISSION DENIED $ ls -l /etc/cron.d /etc/fstab 2>&1 1>/dev/null | tr a-z A-Z ← Just pipe stderr to tr and capitalize LS: CANNOT OPEN DIRECTORY /ETC/CRON.D: PERMISSION DENIED |
$ ls -R /home > redir.txt ← output stdout (the correct part) to the file, but the stderr of the wrong part is not output to the file but to the screen ls: cannot open directory /home/jhon: Permission denied $ ls -R /home 1> redir.txt 2>&1 ← Input all the correct message(stdout) & error message (stderr) to be displayed on the screen to the file, and there will be no output on the screen (use "cat redir. txt" to verify the content of the file) $ ls -R /home 2> redir.txt 2>&1 ←The stderr that was originally output to the file and also output to the screen (stdout & stderr are all output to the screen, so file content is empty) |
$ ls -R /home 2> redir.txt 1>&2 ←The result of this example is the same as "ls -R /home > redir.txt 2>&1" in the above example (think about the reason yourself) $ find / -name '*readme.txt' 1>&2 2>/dev/null ← "1>&2" redirects both stdout and stderr to stderr but "2>/dev/null" also outputs stderr to null, so only stdout is output to the screen |
$ cat << 'EXIT' > redir.txt ← read the data from the keyboard and output it to the file "redir.txt" (it will end automatically after entering "EXIT", and the ending input string can be defined by yourself) > Android's kernel is derived↵ Enter ← Enter any text and press <Enter> > from the Linux kernel.↵ Enter > EXIT↵ Enter ← Meet the end string "EXIT" defined by yourself and press <Enter> to stop input $ cat redir.txt ← Verify Android's kernel is derived from the Linux kernel. $ cat > redir.txt << 'EXIT' ←This example is the same as above |
Syntax: read_from_stdout | tee [-otpiton] filee | ||
command name/function/command user | options | function |
tee/ (pipe tee) T-shaped pipeline/ Any |
-a | output append redirection |
-i | ignore interrupt |
$ echo 'Hello World' | tee tee_file.txt ← both screen and file are output) Hello World ←screen output $ cat tee_file.txt ←Verify The file Hello World |
$ man tee | tee -a file1 file2 file3←tee can split multiple files at one time |
$ cd ~root ←Enter root’s home directory bash: cd: /root: Permission denied ←Permission denied (an error occurred) $ echo $?←Check the command just returned value 1 ←Non-zero means the program execution error $ cd ~ ←Enter your home directory $ echo $? ←Check the value returned by the command just now 0 0 means the program is executed correctly |
# cd /root && mv /root/fileA /root/fileB && clear ←If the execution of "cd" is correct, then execute "mv"; if it is wrong, execute "clear" |
$ cat /etc/man.conf || cat /etc/man.config ← If "cat /etc/man.conf" is wrong, execute "cat /etc/man.config" |
$ [ -f fileA ] && [ -f fileB ] && echo "exist" || echo "Not found" ← If the result of the first command is abnormal, the commands after "&&" will not be executed but "|| "The following command |