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



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

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.

  • 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 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. 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
find plu6_a1 plu61_a2 -type f -iname '*.xml' -print0 | while IFS= read -rd '' f; do
    cp -- "$f" "${dest%/}/${f%.xml}-bk.xml"

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).


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.