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

原文地址:https://www.cnblogs.com/zjd2626/p/7095775.html