Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

# Get length of an array

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

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

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

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 for using $() instead of backticks. – Dennis Williamson Mar 2 '10 at 10:18

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
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
$(COMMAND) and COMMAND result in the same output. – akira Mar 2 '10 at 6:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .