Find and replace files in multiple folders

Hi there,

I would like to write a script to automate the copy and renaming of files in multiple dir.

I have a generic file named s253e.prb and would like to copy this to multiple dir and rename it.

Example:

Dir is AL-M1 and the prb file name is AL-M1.prb. I would like to be able to copy the generic prb file to this dir and rename it AL-M1.prb while backing up the original.

I know that I can use the find command to complete this but I dont know where to start..

The Generic file is kept in the dir S25E3 dir and again we have multiple dirs:

AL-M1
AL-M2
CD-M1

etc

Any starters and help would be grateful to point me in the right dir...

I am using the ksh shell for this

Do you have a list of the directories where the file is to be copied?

Here's my interpretation of your problem:

cd /parent/dir/to/all/work/dirs
for dir in AL-M1 AL-M2 CD-M1; do
   cp -p $dir/${dir}.prb $dir/${dir}.prb.backup
   cp -p /path/to/s253e.prb $dir/${dir}.prb
done

There is 97 dirs that I need to copy and rename the file to. This is why I would like to try and automate the process as much as possible. Is there a way to use the code submitted above but to wildcard the dir input?

Are all the 97 directories in the same path? And are there no other directories in that path? If yes, just do:

cd /parent/dir/to/all/work/dirs
for dir in *; do...

All the directories are in the smae path ie:

/users/clyons/probes/

One other question (the script above works and thanks for it :slight_smile: )

The s25e3.prb file is also contaained within this dir structure. How would I get the scrit to ignore this dir so that an error message is not created
(see attached screen capture)

Again thanks for all your help on this

It isn't an error, just a warning, but you can ignore this directory. Just shove an if in there.

cd /parent/dir/to/all/work/dirs
for dir in *; do
   if [ "$dir" = "s25e3" ]; then   #you can replace this with whatever dir you want to ignore
      continue
   fi
   cp -p $dir/${dir}.prb $dir/${dir}.prb.backup
   cp /path/to/s253e.prb $dir/${dir}.prb
done

Thanks...it works :slight_smile: