[求助]find的-exec -execdir ; +用法

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

[求助]find的-exec -execdir ; +用法

#1

帖子 gre_linewer » 2008-03-09 11:32

1.书上说 +比;好,man 里面也说;是对找到的每个文件都应用一次-exec后的命令,而+是把找到的命令放到一行来处理. 但我用+老是提示出问题:
find . -name '*.txt' -exec cp {} /tmp \+
就提示
find: 遗漏“-exec”的参数
换成find . -name '*.txt' -exec cp {} /tmp \;
find . -name '*.txt' -exec ls {} \+ 都没问题啊 到底哪儿遗漏参数呢?
一直没搞明白

2.对于 工作目录和其子目录下都有一个1.txt, -exec不可同时改名,-execdir就没问题:
find . -name 1.txt -execdir mv {} 2.txt \;
下面两段看了好些遍还不是太了解,哪位可以简单解释下

-exec command ;
The command is executed in the starting directory.
There are unavoidable security problems surrounding use of the -exec option; you should use the -execdir option instead.

-execdir command {} +
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. As with the -exec option, the ’+’ form of -execdir will build a command line to process more than one matched file, but any given invocation of command will only list files that exist in the same subdirectory. If you use this option, you must ensure that your $PATH environment variable does not reference the current directory; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir.
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
头像
windwiny
帖子: 2254
注册时间: 2007-03-13 17:26

#2

帖子 windwiny » 2008-03-09 12:34

你那个 cp 和 ls 的参数位置不一样啊。。。 cp 变成了 /tmp AA BB CC 这样了
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

#3

帖子 gre_linewer » 2008-03-09 15:21

windwiny 写了:你那个 cp 和 ls 的参数位置不一样啊。。。 cp 变成了 /tmp AA BB CC 这样了
楼上的能说的明白点吗?是不是find找到的文件是传到 cp {} /tmp 后面,变成cp /tmp AA BB CC
而不是用找到的文件:AA BB CC 代替{} ?
man里说的: the command line is built by appending each selected file name at the end;

如果我想用+达到这个目的该怎么做?

另外我想在一个目录下(但不想在其一个子目录Txt下)找一个文件1.txt
find . -path './Txt' -prune -a -name 1.txt
find . -wholename './Txt' -prune -a -name 1.txt
都不行,也不报错.
man 里给出的例子只是打印别的文件夹下的内容
find . -wholename ’./src/emacs’ -prune -o -print
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

#4

帖子 gre_linewer » 2008-03-09 15:44

改成find . -path './Txt' -prune -o -name 1.txt就对了
不过我还是不太理解过程是怎样的?
-path ’./Txt’ -prune -o -name 1.txt是-path ’./Txt’ -a -prune -o -name 1.txt的简写,按理说前面的与表达式为真,后面的不执行啊??????????????//
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
xwyxn
帖子: 359
注册时间: 2008-03-30 12:19

Re: [求助]find的-exec -execdir ; +用法

#5

帖子 xwyxn » 2009-01-05 16:51

需要有;结尾
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: [求助]find的-exec -execdir ; +用法

#6

帖子 aerofox » 2009-01-05 21:53

+ 只能跟在 {} 后,即 -exec 要么以 \; 结尾,要么以 {} + 结尾。

代码: 全选

find . -name '*.txt' -exec cp {} /tmp \+ 
改为

代码: 全选

find . -name '*.txt' -exec cp -t /tmp {} + 
就可以了。
回复