find -exec 与xargs 区别


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


-exec
    1.参数是一个一个传递的,传递一个参数执行一次rm
    2.文件名有空格等特殊字符也能处理
-xargs 
    1.一次将参数传给命令,可以使用-n控制参数个数
    2.处理特殊文件名需要采用如下方式:
    find . -name "*.txt" print0 |xargs -0 rm {} 

实验结果如下,可以清楚看到参数传递过程

[root@andes.com ~/tmp/dir]#find . -type f |xargs -t -n 2 echo
echo ./data.txt ./env2.txt 
./data.txt ./env2.txt
echo ./env.txt ./export2.txt 
./env.txt ./export2.txt
echo ./s.txt ./d.txt 
./s.txt ./d.txt
echo ./export.txt ./set.txt 
./export.txt ./set.txt
echo ./fuck.txt 
./fuck.txt
[root@andes.com ~/tmp/dir]#find . -type f -exec echo begin {} \;
begin ./data.txt
begin ./env2.txt
begin ./env.txt
begin ./export2.txt
begin ./s.txt
begin ./d.txt
begin ./export.txt
begin ./set.txt
begin ./fuck.txt
[root@andes.com ~/tmp/dir]#


技巧: find -print0  与 xargs -0 的结合避免文件名有特殊字符如空格,引号等无法处理:

 find . -name "*.txt" print0 |xargs -0 rm {} 

find 

       -print True; print the full file name on the standard output, followed by a newline.   If you are piping the
              output of find into another program and there is the faintest possibility that the  files  which  you
              are  searching  for  might  contain  a  newline, then you should seriously consider using the -print0
              option instead of -print.  See the UNUSUAL FILENAMES section for information about how unusual  char-
              acters in filenames are handled.


       -print0
              True;  print  the full file name on the standard output, followed by a null character (instead of the
              newline character that -print uses).  This allows file names that contain newlines or other types  of
              white space to be correctly interpreted by programs that process the find output.  This option corre-
              sponds to the -0 option of xargs.

xargs 

       -0     Input items are terminated by a null  character  insteadof  by
     whitespace,  and the quotes and backslash are not special (every
     character is taken literally).  Disables the end of file string,
     which  istreated  like any other argument.  Useful when input
     items might contain white space, quote  marks,  or  backslashes.
     The  GNU find  -print0  option produces input suitable for this
     mode.



### 回答1: 在Linux下,find命令可以用来查找文件和目录。在find命令的基础上,可以使用xargs和-exec来进一步处理查找到的结果。 xargs命令可以将find命令查找到的结果作为参数传递给其他命令。例如,可以使用以下命令将查找到的所有文件删除: ``` find /path/to/dir -type f -name "*.txt" | xargs rm ``` 这个命令会查找/path/to/dir目录下所有扩展名为.txt的文件,并将它们作为参数传递给rm命令,从而删除这些文件。 -exec命令可以在find命令查找到的每个文件上执行指定的命令。例如,可以使用以下命令将查找到的所有文件复制到另一个目录: ``` find /path/to/dir -type f -name "*.txt" -exec cp {} /path/to/another/dir \; ``` 这个命令会查找/path/to/dir目录下所有扩展名为.txt的文件,并将它们复制到/path/to/another/dir目录中。 总的来说,xargs和-exec命令可以帮助我们更方便地处理find命令查找到的结果。需要注意的是,使用这些命令时要注意参数的正确性和安全性。 ### 回答2: 在Linux系统中,find命令可以查找指定目录下的文件和文件夹,其语法如下所示: ```shell find path -options [expressions] ``` 其中,path表示要查找的目录路径,-options则是一系列选项,用于指定查找的规则,expressions则是查找表达式。 在find命令中,使用-xargs和-exec选项可以在查找出符合条件的文件后执行相应操作。这两个选项的主要区别在于,-exec选项将查找到的文件作为参数传递给执行的命令,而-xargs选项则将它们作为标准输入流传递给命令。 举个例子,假设我们要查找当前目录下所有的.txt文件,并将它们全部重命名为_new.txt。我们可以使用以下命令: ```shell find . -name "*.txt" -exec mv {} {}_new.txt \; ``` 其中,-name选项用于指定查找的文件名模式,-exec选项则用于执行重命名操作,{}表示查找到的文件名,由于在命令行中{}是一个特殊的字符,因此需要使用\进行转义,最后一个分号表示命令的结束。 如果使用-xargs选项,我们需要将命令改为: ```shell find . -name "*.txt" | xargs -I {} mv {} {}_new.txt ``` 其中,|表示将find命令的输出作为xargs命令的输入,-I选项用于指定替换字符串(这里为{}),mv命令则将每个文件重命名为_new.txt。 需要注意的是,在使用-xargs选项时,对于包含空格等特殊字符的文件名,需要使用-0选项将它们用空字符分隔,如下所示: ```shell find . -name "*.txt" -print0 | xargs -0 -I {} mv {} {}_new.txt ``` 总的来说,使用-exec选项简单直观,而-xargs选项则更灵活,可以将查找到的文件批量传递给命令进行处理。因此,在实际运用中,我们可以根据具体情况选择合适的选项。 ### 回答3: 在Linux系统中,有两个常见的命令用于在文件系统中查找文件:find和xargs。这两个命令可以用来找到符合特定条件的文件,但是它们的功能略有不同。 find命令是一个非常强大的工具,它可以用来查找任意目录下符合指定条件的文件或目录。find命令的语法为: find [path] [expression] 其中path表示要查找的目录或文件,而expression是由各种测试和动作组成的语句。 xargs命令则是用来接收一个命令的输出作为输入,并将其传递给另一个命令。xargs命令的常见用法是将find命令的输出作为xargs的输入,并将输出交给另一个命令来处理。 在使用find命令和xargs命令时,有两种常见的用法。第一种是使用find命令和-exec选项来执行一个命令,第二种是使用find命令和xargs命令来与其他工具一起使用。 使用find命令和-exec选项来执行命令 在使用find命令时,最常见的选项是-exec。该选项允许您执行一个命令,以处理每个find找到的文件。下面是一个示例: find /home -name "*.txt" -exec cat {} \; 此命令将在/home目录下查找所有扩展名为.txt的文件,并将它们的内容输出到终端。 使用find命令和xargs命令与其他工具一起使用 find命令的输出可以用作xargs命令的输入。在这种情况下,xargs命令将用每个换行符分隔的输出作为参数,并将其传递给指定的命令。以下示例演示了这种用途: find /home -name "*.txt" | xargs grep "keyword" 此命令将在/home目录下查找所有扩展名为.txt的文件,并将它们的内容传递给grep命令以查找关键字。 总之,find命令和xargs命令都是Linux系统中常用的工具,可以用于查找文件和将一组文件传递给其他命令进行处理。使用它们可以简化许多常见的任务,并增强您在Linux系统中的生产力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值