首页 > 系统服务 > 详细

Linux 重定向与管道

时间:2015-05-08 23:47:17      阅读:416      评论:0      收藏:0      [点我收藏+]

标准输出重定向

">" 操作符:覆盖目标文件内容

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

">>" 操作符:在目标文件尾部追加输出内容

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

 

标准错误重定向

"2>" 操作符:覆盖目标文件内容

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

"2>>" 操作符:在目标文件尾部追加输出内容

huey@huey-K42JE:~/huey/linux/cmdline$ ls inexistent_dir 2>> ls-error.txt 
huey@huey-K42JE:~/huey/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

 

将标准输出与标准错误重定向到同一文件

"&>" 操作符:覆盖目标文件内容

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

"&>>" 操作符:在目标文件尾部追加输出内容

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

 

标准输入重定向

"<" 操作符

huey@huey-K42JE:~/huey/linux/cmdline$ echo ‘hello world‘ > foo
huey@huey-K42JE:~/huey/linux/cmdline$ cat < foo
hello world

 

管道

"|" 操作符

huey@huey-K42JE:~/huey/linux/cmdline$ ls /usr/bin | grep ‘^zip‘
zip
zipcloak
zipgrep
zipinfo
zipnote
zipsplit

 

Linux 重定向与管道

原文:http://www.cnblogs.com/huey/p/4487021.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!