linux将上一条命令的结果作为下一条命令的参数

1、xargs

[root@localhost test]# touch test{1..10}.txt
[root@localhost test]# ls
test10.txt  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test6.txt  test7.txt  test8.txt  test9.txt
[root@localhost test]# ls /test/|grep -v test3                   #查找不包含test3的文件
test10.txt
test1.txt
test2.txt
test4.txt
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
[root@localhost test]# ls /test/|grep -v test3|xargs rm -rf       #删除不包含test3的文件
[root@localhost test]# ls
test3.txt 

2、使用反引号

[root@localhost test]# touch aa{1..10}.txt
[root@localhost test]# ls
aa10.txt  aa1.txt  aa2.txt  aa3.txt  aa4.txt  aa5.txt  aa6.txt  aa7.txt  aa8.txt  aa9.txt
[root@localhost test]# rm -rf `ls /test/|grep -v aa2`#将反引号内的结果作为下一条命令的参数
[root@localhost test]# ls
aa2.txt

3、使用find exec命令

[root@localhost test]# ls
aa2.txt
[root@localhost test]# find /test/ -name  aa2.txt -exec rm -rf {} \; #这里的{}和\;是成对使用的。#将find的
                                                                     #查找结果作为参数

4、使用$( )

[root@localhost test]# touch db{1..10}
[root@localhost test]# ls
db1  db10  db2  db3  db4  db5  db6  db7  db8  db9
[root@localhost test]# rm -rf $(ls /test|grep -v db2) 将$( )的执行结果作为rm -rf 的参数
[root@localhost test]# ls
db2
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐