JosiP
JosiP

Reputation: 225

using find with exec

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

Upvotes: 1

Views: 2277

Answers (4)

ghostdog74
ghostdog74

Reputation: 342363

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

Upvotes: 1

RandomNickName42
RandomNickName42

Reputation: 5955

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

Upvotes: 0

codaddict
codaddict

Reputation: 455010

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.

Upvotes: 1

CodeRain
CodeRain

Reputation: 6582

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

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

Upvotes: 0

Related Questions