Monday 6 May 2013

UNIX: Copy a file to multiple subdirectories

I have a directory tree and in each of the lowest level directories, I
have a file called "attributes"

I've updated the contents of one of them, and want to copy it to the
other directories.

1. Find the files
find . -name attributes > target_directories.log

2. Edit "target_directories.log" and remove the "attributes" so it is
a list of subdirectories.

./building_trades/carpentry/attributes
./building_trades/concreting_paving/attributes
./party_catering/attributes

becomes

./building_trades/carpentry/
./building_trades/concreting_paving/
./party_catering/

Do this in vi using:

:%s/search_string/replacement_string/g

:%s/\/attributes/\//g

We have to use the / in our search and replace, as doing

:%s/attributes//g

didn't seem to work

The "\" has to be there to escape the "/" character

3. Copy our updated file to other subdirectories:

cat target_directories.log |xargs -n 1 cp attributes

No comments: