6

I ran a perl script using

perl -p -i.bak -e "..." *.sh dir/*.sh

This created a copy of every file like

script.sh
script.sh.bak

I now want to restore from the .bak files. How can I do this easily?

Baruch
  • 20,590
  • 28
  • 126
  • 201

3 Answers3

7
for file in *.sh.bak dir/*.sh.bak; do cp "$file" "${file//.bak}"; done

Or you could use mv instead of cp.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Op De Cirkel: Why do you say it won't work? I believe the shell can handle any amount of files as the result of wildcard expansion; it's when you try to pass a long list to another process that `ARG_MAX` could bite you. – tripleee May 29 '12 at 09:22
  • 1
    @OpDeCirkel: [`ARG_MAX` doesn't apply to shell builtins](http://stackoverflow.com/questions/6461227/limits-of-length-for-the-output-of-in-bash) – Dennis Williamson May 29 '12 at 12:24
  • @tripleee: You are correct. See my comment to Op De Cirkel above. – Dennis Williamson May 29 '12 at 12:24
  • @Dennis Williamson You are right, ARG_MAX does not apply to built-ins....Good to know – Op De Cirkel May 29 '12 at 19:19
2

find -name '*.bak'|sed 's:.bak$::'|xargs -n 1 -I % cp %.bak %

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
0

Thanks for this fix.

I've been using a combo the code below to fix an 'eval(base64_decode' in Joomla sites.

find: grep -lr --include=*.php "eval(base64_decode" ./

remove: grep -lr --include=.php "eval(base64_decode" ./ | xargs sed -i.bak 's/eval(base64_decode[^;];/\n/g'

undo: find -name '*.bak'|sed 's:.bak$::'|xargs -n 1 -I % cp %.bak %

Lionel Morrison
  • 566
  • 4
  • 15