本文由老男孩教育首发
转载请注明出处:https://www.oldboyedu.com/zuixin_wenzhang/index/id/269.html
find命令结合exec和xargs使用的区别:
使用-exec选项命令操作示例及结果如下:
[root@oldboy ~]# find . -type f -exec echo oldboyedu { } \ ; #<==从命令执行的结果可以看到,每次获得一个文件就输出一次。 oldboyedu ./.viminfo oldboyedu ./anaconda-ks.cfg oldboyedu ./install.log oldboyedu ./install.log.syslog oldboyedu ./.bash_logout oldboyedu ./.cshrc oldboyedu ./ls.txt oldboyedu ./.bash_history oldboyedu ./.lesshst oldboyedu ./oldboy. Log oldboyedu ./test. txt oldboyedu ./.tcshrc oldboyedu ./GB2312.txt oldboyedu ./.bash_profile oldboyedu ./.bashrc
使用xargs命令操作示例及结果如下:
[root@oldboy ~] # find . -type f | xargs echo oldboyedu #<==输出结果只有一行,xargs 获取到所有文件名一次性输出。 oldboyedu ./.viminfo ./anaconda-ks.cfg ./install.log ./install.log.syslog ./.bash_logout ./.cshrc ./ls.txt ./.bash_history ./.lesshst ./oldboy.log ./test.txt ./.tcshrc ./GB2312.txt ./.bash_profile ./.bashrc
xargs还能控制每行输出的参数个数,示例如下,更多使用方法见xargs命令。
[root@oldboy ~] # find . -type f | xargs -n 3 echo oldboyedu #<==使用-n 3指定每次输出3个参数。 oldboyedu ./.viminfo ./anaconda-ks.cfg ./install.log oldboyedu ././install.log.syslog ./.bash_logout ./.cshrc oldboyedu ./ls.txt ./.bash_history ./.lesshst oldboyedu ./oldboy.log ./test.txt ./.tcshrc oldboyedu ./GB2312.txt ./.bash_profile ./.bashrc
验证区别二的案例:
[root@oldboy ~]# touch “oldboy edu” #<==创建一个文件名带有空格的特殊文件。 [root@oldboy ~]# 11 -h “oldboy edu” -rw-r--r-- 1 root root 0 May 17 16:30 oldboy edu [root@oldboy ~] # find . -name "*oldboy*" -exec ls -lh {} \; #<==使用 -exec 参数正常使用。 -rw-r--r-- 1 root root 0 May 17 16:30 ./oldboy edu [root0oldboy ~] # find . -name "*edu*" | xargs ls -lh #<==使用 xargs 命令无法正常打印 ls: cannot access ./oldboy: No such file or directory ls: cannot access edu: No such file or directory [root@oldboy ~] # find . -name "*edu*" -print0| xargs -0 ls -lh -rw-r--r-- 1 root root 0 May 17 16:30 . /oldboy edu
转载于:https://blog.51cto.com/12306609/2169641