其他分享
首页 > 其他分享> > xargs和find

xargs和find

作者:互联网

[root@localhost shell]# cat /tmp/xargs
1 2 3
4 5 6 7 8
9 10

[root@localhost shell]# cat /tmp/xargs |xargs
1 2 3 4 5 6 7 8 9 10
[root@localhost shell]# cat /tmp/xargs |xargs -n 3    #-n参数为划分为多行
1 2 3
4 5 6
7 8 9
10
[root@localhost shell]#

 

xargs   -d选项为输入指定一个定界符,默认是空格

[root@localhost shell]# echo "pwdAcatAtreeABC" |xargs -d A
pwd cat tree BC

[root@localhost shell]# echo "pwdAcatAtreeABC" |xargs -d A -n 2
pwd cat
tree BC

 

 结合find和xargs

[root@localhost tmp]# ll
total 20
drwxr-xr-x. 2 root root 6 Oct 2 13:49 consul-test
drwxr-xr-x. 3 root root 18 Oct 6 16:06 data
drwxr-xr-x. 2 root root 6 Oct 6 15:47 data1
d-wx-wx-wx. 2 root root 6 Oct 6 15:48 data2
-rw-r--r--. 1 root root 25 Oct 18 09:03 nusers.sh
drwx------. 3 root root 17 Oct 14 10:25 systemd-private-7cacc66a24ab427a83d19076e73213b6-httpd.service-d9KtA4
drwxr-xr-x. 2 root root 6 Oct 6 15:46 test
-rw-r--r--. 1 root root 89 Oct 18 11:40 total-1.sh
-rw-r--r--. 1 root root 91 Oct 18 13:26 total-2.sh
-rw-r--r--. 1 root root 82 Oct 18 11:08 total.sh
-rw-r--r--. 1 root root 22 Oct 18 16:15 xargs
[root@localhost tmp]# find /tmp -type f name "*.sh"
find: paths must precede expression: name
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@localhost tmp]# find /tmp -type f -name "*.sh"
/tmp/nusers.sh
/tmp/total.sh
/tmp/total-1.sh
/tmp/total-2.sh
[root@localhost tmp]# find . -type f -name "*.sh"
./nusers.sh
./total.sh
./total-1.sh
./total-2.sh

[root@localhost tmp]# find . -type f -name "*.sh" -print0 | xargs -0
./nusers.sh ./total.sh ./total-1.sh ./total-2.sh
[root@localhost tmp]# find . -type f -name "*.sh" -print0 | xargs -0 rm -f     #用find匹配出/tmp目录下.sh文件,然后用xargs将这些删除
[root@localhost tmp]# ll
total 4
drwxr-xr-x. 2 root root 6 Oct 2 13:49 consul-test
drwxr-xr-x. 3 root root 18 Oct 6 16:06 data
drwxr-xr-x. 2 root root 6 Oct 6 15:47 data1
d-wx-wx-wx. 2 root root 6 Oct 6 15:48 data2
drwx------. 3 root root 17 Oct 14 10:25 systemd-private-7cacc66a24ab427a83d19076e73213b6-httpd.service-d9KtA4
drwxr-xr-x. 2 root root 6 Oct 6 15:46 test
-rw-r--r--. 1 root root 22 Oct 18 16:15 xargs
[root@localhost tmp]#

 

 

 

标签:tmp,xargs,Oct,sh,root,find,localhost
来源: https://www.cnblogs.com/liutaolt/p/15421279.html