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...
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?
One other question (the script above works and thanks for it )
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)
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