使用find与exec

时间:2010-01-07 10:48:02

标签: bash find

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

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

最好的问候

4 个答案:

答案 0 :(得分:1)

如果您有Bash 4.0并假设您找到了txt文件

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

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

或其他版本的GNU find + bash

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

如果你没有GNU find

那么这个丑陋的那个
$ find /path -name '*.txt' -type f -exec basename {} \; | xargs -I file echo cp /path/file /destination/test_file

答案 1 :(得分:1)

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

假设您所在的目录中包含要复制的文件,而test_path是其子目录。

答案 2 :(得分:0)

您应该将整个test_path/"test_"'{}'放入"" 像:

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

答案 3 :(得分: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

自然地删除了最后一部分的回声,所以它不仅仅是回应它做了什么并运行cp