1

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

3
  • AHHAHAH wow i think all of us who answreed this one should get a badge for like "most people with simular answer to same question at the same time" Commented Jan 7, 2010 at 11:13
  • and by simular i mostly just mean that we all used a for loop and managed the operation with a temp variable Commented Jan 7, 2010 at 11:16
  • -exec isn't what you want to use here. The purpose of "-exec" is to run a command on each file to determine if it should be included in find's list of results, this is why grep is the most common example for this usage. Commented Jan 11, 2010 at 12:52

4 Answers 4

1
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

any ideas how to use it without enetring the path ?
@ghostdog74: you forgot the "/test" in the first find version.
@dennis, thanks. I have edited, but if its wrong again, pls do the edit for me. thks. @JosiP, what OS are you on? Solaris?
0

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

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

3 Comments

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
But if {} is a complete path (as it so often is with find) will "test_path/test_{}" mean what you want?
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.
0

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

1 Comment

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!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.