管道 | 和重定向符

1. 管道 |

管道|就是用来将符号|前的命令输出作为符号后命令的输入,多个命令的顺序一定是从左到右顺序执行,下面用几个例子来解释。

  • ps aux | grep chrome: 查看chrome浏览器的进程信息。利用ps aux获取的结果,在结果里搜索带chrome字段的行就可以得到chrome进程信息。
➜  linux_commands ps aux | grep chrome
qiushye          30641   0.0  0.0  4268280    548 s000  R+   12:12PM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn chrome
qiushye            598   0.0  0.0  4308152    256   ??  S    24Mar20   0:01.46 /Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/80.0.3987.149/Helpers/chrome_crashpad_handler --monitor-self-annotation=ptype=crashpad-handler --database=/Users/qiushye/Library/Application Support/Google/Chrome/Crashpad --metrics-dir=/Users/qiushye/Library/Application Support/Google/Chrome --url=https://clients2.google.com/cr/report --annotation=channel= --annotation=plat=OS X --annotation=prod=Chrome_Mac --annotation=ver=80.0.3987.149 --handshake-fd=7
  • ls | wc -l: 查看当前目录下的文件和目录个数
➜  linux_commands ls | wc -l
      11
➜  linux_commands ls
diff.txt  input.txt sl_test4  test1     test4     ut
head.txt  ls.txt    sl_ut     test3     tt
  • grep d test3 | grep o: 在test3文件中找出既带"d"又带"o"的行
➜  linux_commands cat test3
hello world!


oh my god!
你是

ss
➜  linux_commands grep d test3 | grep o
hello world!
oh my god!
  • ls | sort: 对目录下的文件按名称排序展示
➜  linux_commands ls | sort
diff.txt
head.txt
input.txt
ls.txt
sl_test4
sl_ut
test1
test3
test4
tt
ut

2. 重定向>, >>, <

2.1 command > file: 将命令command的输出重定向到文件file中

  • echo [text] > [file]: 将一段文本内容[text]写入文件[file]中
➜  linux_commands echo "ls" > ls.cmd
➜  linux_commands cat ls.cmd
ls
  • cat [file1] > [file2]: 将文件[file1]的内容写入到文件[file2]中
➜  linux_commands ls test5
ls: test5: No such file or directory
➜  linux_commands cat test1 > test5
➜  linux_commands diff test1 test5
➜  linux_commands ls -l test1 test5 (只复制内容,元数据独立)
-rwxrwxrwx  1 qiushye  staff  47 Apr  4 22:24 test1
-rw-r--r--  1 qiushye  staff  47 Apr 10 12:40 test5
  •  grep [text] [file1] > [file2]: 将文件[file1]查找文本[text]的行写入到文件[file2]中
➜  linux_commands grep o test1 > test6
➜  linux_commands cat test6
hello world!
oh my god!

2.2 comand >> file: 将命令command的输出以追加的方式重定向到文件file中

  • echo [text] >> [file]: 将一段文本内容[text]追加写入文件[file]中
➜  linux_commands cat ls.cmd
ls
➜  linux_commands echo "ls -l" >> ls.cmd
➜  linux_commands cat ls.cmd
ls
ls -l
  • cat, grep或其他命令也是类似,此处不多展示,用法和>类似 

2.3 command < file: 将命令command的输入重定向到file,即以file作为command的输入

  • cat < [file] == cat [file]
➜  linux_commands cat < ls.cmd
ls
ls -l
  • grep [text] < [file1] > [file2]: 将文件[file1]中查找文本[text]的行写入文件[file2]中
➜  linux_commands grep h < test1 > test7
➜  linux_commands cat test7
hello world!
oh my god!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值