首頁 > 軟體

Linux 重定向與管道

2020-06-16 18:00:43

標準輸出重定向

">" 操作符:覆蓋目標檔案內容

linuxidc@linuxidc:~/linuxidc/linux/cmdline$ date > foo
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat foo
Fri May 8 09:55:42 CST 2015

">>" 操作符:在目標檔案尾部追加輸出內容

linuxidc@linuxidc:~/linuxidc/linux/cmdline$ date >> foo
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat foo
Fri May 8 09:55:42 CST 2015
Fri May 8 09:57:02 CST 2015

標準錯誤重定向

"2>" 操作符:覆蓋目標檔案內容

linuxidc@linuxidc:~/linuxidc/linux/cmdline$ ls inexistent_dir 2> ls-error.txt 
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat ls-error.txt 
ls: cannot access inexistent_dir: No such file or directory

"2>>" 操作符:在目標檔案尾部追加輸出內容

linuxidc@linuxidc:~/linuxidc/linux/cmdline$ ls inexistent_dir 2>> ls-error.txt 
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat ls-error.txt 
ls: cannot access inexistent_dir: No such file or directory
ls: cannot access inexistent_dir: No such file or directory

將標準輸出與標準錯誤重定向到同一檔案

"&>" 操作符:覆蓋目標檔案內容

linuxidc@linuxidc:~/linuxidc/linux/cmdline$ date &> foo
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat foo
Fri May 8 10:16:12 CST 2015
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ ls inexistent_dir &> foo 
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat foo
ls: cannot access inexistent_dir: No such file or directory

"&>>" 操作符:在目標檔案尾部追加輸出內容

linuxidc@linuxidc:~/linuxidc/linux/cmdline$ echo 'hello world' > foo
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ date &>> foo
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ ls inexistent_dir &>> foo 
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat foo
hello world
Fri May 8 10:25:24 CST 2015
ls: cannot access inexistent_dir: No such file or directory

 

標準輸入重定向

"<" 操作符

linuxidc@linuxidc:~/linuxidc/linux/cmdline$ echo 'hello world' > foo
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ cat < foo
hello world 

管道

"|" 操作符

 
linuxidc@linuxidc:~/linuxidc/linux/cmdline$ ls /usr/bin | grep '^zip'
zip
zipcloak
zipgrep
zipinfo
zipnote
zipsplit

本文永久更新連結地址http://www.linuxidc.com/Linux/2015-05/117234.htm


IT145.com E-mail:sddin#qq.com