tags:

views:

119

answers:

4

Hi

I want to copy files found by find (with exec cp option) but, i'd like to change name of those files - e.g find ... -exec cp '{}' test_path/"test_"'{}' , which to my test_path should copy all files found by find but with prefix 'test'. but it ain't work.

I'd be glad if anyone could give me some ideas how to do it.

best regards

A: 

You should put the entire test_path/"test_"'{}' in "" Like:

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

Gautam Borad
This does not actually work as {} takes the whole find as result so you will get test_path/test_yourfolder/file.txt from /yourfolder/file.txt
Calmar
But if {} is a complete path (as it so often is with `find`) will "test_path/test_{}" mean what you want?
pavium
I assumed that OP wanted to copy each file to a test_path dir wherever the file was found.So i went on that line of thought.
Gautam Borad
+1  A: 

if you have Bash 4.0 and assuming you are find txt files

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

of with GNU find

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

OR another version of GNU find+bash

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

OR this ugly one if you don't have GNU find

$ find /path -name '*.txt' -type f -exec basename {} \; | xargs -I file echo cp /path/file /destination/test_file
ghostdog74
any ideas how to use it without enetring the path ?
JosiP
see the third version
ghostdog74
@ghostdog74: you forgot the "/test" in the first find version.
Dennis Williamson
got bash 3.0. my find does not accept 'printf option' :(
JosiP
@dennis, thanks. I have edited, but if its wrong again, pls do the edit for me. thks.@JosiP, what OS are you on? Solaris?
ghostdog74
i got sunos 5.10
JosiP
+1  A: 
for i in `find . -name "FILES.EXT"`; do cp $i test_path/test_`basename $i`; done

It is assumed that you are in the directory that has the files to be copied and test_path is a subdir of it.

codaddict
A: 

I would break it up a bit, like this;

 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

Here's the output;

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

Naturally remove the echo from the last part, so it's not just echo'ng what it woudl of done and running cp

RandomNickName42
hoho! but i vote for my simular solution as the most flexiable, does not require current dir and can cp into an arbitrially deep/complex dest folder!! ;) however ya it may be the slowest, maybe not if you got lotsa core's tho!!
RandomNickName42