Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have some dirs with different plugin-cfg.xml files that I want to take a backup of and keep in the same dir.

E.g.

Plug6_a1/plugin-cfg.xml
Plug6_a2/plugin-cfg.xml
Plug6_a3/plugin-cfg.xml

How can I make a copy of plugin-cfg.xml rename to plugin-cfgbk.xml for each directory?

share|improve this question
add comment

3 Answers

find Plu*_a* -type f -name plugin-cfg.xml -exec \
  sh -c 'cp "$1" "$(dirname "$1")"/plugin-cfgbk2.xml ' sh {} \;  

For every file named plugin-cfg.xml located under a directory which name starts with Plu and contains _a, make a copy of plugin-cfg.xml to plugin-cfgbk2.xml in the same directory.

share|improve this answer
    
Sorry there's a couple of mistakes on my part there. Plug6_a1/plugin-cfg.xml Plug6_b2/plugin-cfg.xml Plug6_c3/plugin-cfg.xml Make a copy of plugin-cfg.xml rename to plugin-cfgbk.xml for all dir –  scott marriott May 17 '13 at 21:05
    
and please better explain "Make a copy of plugin-cfg.xml". Where should this copy go ? Are all the xml files named plugin-cfg.xml ? –  jlliagre May 17 '13 at 21:45
    
Yeah I wanted to copy it into the same dir so the orignal and backup are there. all plugin-cfg.xml are named the same but have different configurations in them. Thanks for advise and will amend the question. –  scott marriott May 17 '13 at 22:02
    
Can the down-voter explain what he thinks is wrong in my reply ? –  jlliagre May 28 '13 at 7:37
add comment
dest=/path/to/backup/destination;
find plu6_a1 plu61_a2 -type f -iname '*.xml' -print0 | while IFS= read -rd '' f; do
    cp -- "$f" "${dest%/}/${f%.xml}-bk.xml"
done

Find every XML file in plu6_a1 plu61_a2, make a copy, add the suffix -bk.xml to the copy and move it to "$dest".

EDIT: This code is written in Bash and it also requires GNU or BSD find(1).

share|improve this answer
add comment

Here's a one-liner that requires only /bin/sh (this only works for a single directory, however):

for f in ./*.xml; do cp "$f" "${f%cfg.xml}cfgbk.xml"; done

Assuming you have bash 4+ (if you don't know, you probably do; check with bash --version), you can use globstar for recursiveness:

shopt -s globstar
for f in ./**/*.xml; do cp "$f" "${f%cfg.xml}cfgbk.xml"; done

You might want to consider a clearer naming scheme for your backups, however -- I would prefer something like appending '.backup' to the filenames.

shopt -s globstar
for f in ./**/*.xml; do cp "$f" "$f".backup; done
share|improve this answer
add comment

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.