2010-01-07 39 views
1

我想复制查找发现的文件(与exec cp选项),但是,我想要更改这些文件的名称 - 例如find ... -exec cp '{}' test_path/"test_"'{}',这对我的test_path应该复制find找到的所有文件,但与前缀'测试'。但它不起作用。使用查找与执行

如果有人能给我一些想法如何去做,我会很高兴。

问候

+0

AHHAHAH哇,我想我们所有的人谁answreed这一个应该得到一个徽章像“大多数人同时回答相同的问题” – RandomNickName42 2010-01-07 11:13:56

+0

和simular我主要只是说我们都使用for循环,并用临时变量管理操作 – RandomNickName42 2010-01-07 11:16:43

+0

-exec不是你想在这里使用的。 “-exec”的目的是在每个文件上运行一个命令来确定它是否应该包含在find的结果列表中,这就是为什么grep是这种用法最常见的例子。 – alxp 2010-01-11 12:52:41

回答

0

你应该把整个在test_path/"test_"'{}'"" 像:

find ... -exec cp "{}" "test_path/test_{}" \;

+0

这实际上并不工作,因为{}将整个查找作为结果,所以您将从/yourfolder/file.txt获取test_path/test_yourfolder/file.txt – Calmar 2010-01-07 11:03:28

+0

但如果{}是完整路径(因为它经常与“ find')将“test_path/test_ {}”表示你想要什么? – pavium 2010-01-07 11:05:33

+0

我假设OP想要将每个文件复制到test_path目录,无论文件在哪里找到。 所以我继续这一思路。 – CodeRain 2010-01-07 11:12:56

1

,如果你有猛砸4.0和假设你是找txt文件

cd /path 
for file in ./**/*.txt 
do 
    echo cp "$file" "/test_path/test${file}" 
done 

的与GNU发现

find /path -type f -iname "*.txt" | while read -r -d"" FILE 
do 
    cp "$FILE" "test_${FILE}" 
done 

或者GNU的另一个版本找到+庆典

find /path -type f -name "*txt" -printf "cp '%p' '/tmp/test_%f'\n" | bash 

,或者如果你没有GNU找到

$ find /path -name '*.txt' -type f -exec basename {} \; | xargs -I file echo cp /path/file /destination/test_file 
+0

任何想法如何使用它没有enetring的路径? – JosiP 2010-01-07 11:55:54

+0

看到第三个版本 – ghostdog74 2010-01-07 12:11:14

+0

@ ghostdog74:你在第一个查找版本中忘记了“/ test”。 – 2010-01-07 12:11:19

1
for i in `find . -name "FILES.EXT"`; do cp $i test_path/test_`basename $i`; done 

假设你在这个丑具有要复制的文件的目录和test_path是其子目录。

0

我会分解它,像这样;

for line in `find /tmp -type f`; do FULL=$line; name=`echo $line|rev|cut -d/-f -1|rev` ; echo cp $FULL "new/location/test_$name" ;done 

这是输出;

cp /tmp/gcc.version new/location/test_gcc.version 
cp /tmp/gcc.version2 new/location/test_gcc.version2 

当然要从最后一部分的回声,所以它不只是echo'ng什么woudl完成,运行CP的

+0

hoho!但我投我的simular解决方案作为最灵活的,不需要当前目录,并可以cp到一个任意深/复杂的dest文件夹!;)但是它可能是最慢的,也许不是如果你有很多核心的寿! – RandomNickName42 2010-01-07 11:21:47