Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In solaris, I'd like to copy all files found by the find command to a slightly different path. The following script basically executes cp for each file found by find. For example:

cp ./content/english/activity1_compressed.swf ./content/spanish/activity1_compressed.swf
cp ./content/english/activity2_compressed.swf ./content/spanish/activity2_compressed.swf
...

#!/bin/bash

# Read all file names into an array
FilesArray=($(find "." -name "*_compressed.swf"))

# Get length of an array
FilesIndex=${#FilesArray[@]}

# Copy each file from english to spanish folder
# Ex: cp ./english/activity_compressed.swf ./spanish/activity_compressed.swf
for (( i=0; i<${FilesIndex}; i++ ));
do
    source="${FilesArray[$i]}"

    # Replace "english" with "spanish" in path  
    destination="$(echo "${source}" | sed 's/english/spanish/')"

    cp "${source}" "${destination}"
done

exit 0;

It seems like a bit much and I wonder how to use sed in-line with the find and cp commands to achieve the same thing. I would have hoped for something like the following, but apparently parentheses aren't acceptable method of changing order of operation:

find . -name *_compressed -exec cp {} (echo '{}' | sed 's/english/spanish/')
share|improve this question
1  
+1 for using $() instead of backticks. –  Dennis Williamson Mar 2 '10 at 10:18

2 Answers 2

There are easier ways, but for portability sake we can use a bit of forking and backticks:

find . -name *_compressed -exec sh -c 'cp {} `echo {} | sed 's/english/spanish/'`' \;
share|improve this answer
    
Great! +1 The shell maniac! :D Can you give me some hints about the easier ways you was referring at? Maybe those ways will also accomplish better to that purpose. –  dag729 Mar 2 '10 at 4:53
2  
I'm not sure on his exact environment, but if all of the files are only within 1 directory, he could cd there then just cp *_compressed.swf ../spanish –  John T Mar 2 '10 at 4:58
    
i was thinking the same but instead of 'cding' there he can just give it to 'cp' as the target directory. –  akira Mar 2 '10 at 6:14
    
When I make a test run on OS X, -exec sh -c 'cp {} ... works, but if I run on solaris... replacing cp with echo for safety. Output is "{}" I'm guessing that -exec sh -c doesn't work on solaris. –  Michael Prescott Mar 2 '10 at 18:12
    
@John T: All of the files aren't in 1 directory. –  Michael Prescott Mar 2 '10 at 18:13

I'd use the backtick: you'll find more upon it here (backtick is at chapter 3.4.5).

Basically when you enclose a command between backticks the command itself will be substituted in his output.

share|improve this answer
1  
$(COMMAND) and COMMAND result in the same output. –  akira Mar 2 '10 at 6:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.