linux exec和xargs的區別


-exec
    1.參數是一個一個傳遞的,傳遞一個參數執行一次,效率低
    2.文件名有空格等特殊字符也能處理
-xargs  
    1.一次將參數傳給命令,可以使用-n控制參數個數
    2.處理特殊文件名需要采用如下方式:find . -name "*.txt" -print0 |xargs -0 rm {}  
技巧: find -print0  與 xargs -0 的結合避免文件名有特殊字符如空格,引號等無法處理:
    3.有些命令不支持多個參數,需要用-n 1

eg:

mkdir test

cd test

touch {1..10000}.txt

vi test.sh

#!/bin/bash
echo "There is $# parameters."
echo "rm $@"
rm "$@"
echo "PID is $$"

find . -name "*.txt"  -exec echo {} \;
find . -name "*.txt" |xargs  echo

find . -name "*.txt" |xargs -n 1 echo

find . -regextype posix-egrep -regex "./[0-9]{1,5}.txt" -exec ./test.sh {} \;

find . -regextype posix-egrep -regex "./[0-9]{1,5}.txt" -exec ./test.sh {} +

find . -regextype posix-egrep -regex "./[0-9]{1,5}.txt" |xargs ./test.sh

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM