0

So I tried to use this script to get rid of a username in a bunch of files

#!/bin/bash ls | while read -r FILE do mv -v "$FILE" `echo $FILE | tr '[DarkDream]' '.' ` done 

Instead, all of the files that had the letters d, a, r, k, e and m got replace with a.

Is there a way to undo this?

5
  • I assume by now that you've done 'man tr' and realized that tr replaces (i.e. "TRanslates") from one character SET to another set, and does not replace strings? You should have used: mv -v "$FILE" echo "$FILE" | sed -e "s/DarkDream//" Commented Aug 8, 2015 at 21:01
  • @Bytor A better solution would have been to use Robin Barker's rename (packaged by many distributions) like so: rename 's/DarkDream//' * Commented Aug 8, 2015 at 22:38
  • Do you still have the output from running that script? If so, it might very well be possible to make a script that undoes the damage, because with -v, mv will print the original and new file name for every file that gets moved or renamed. Commented Aug 8, 2015 at 22:40
  • You say "all of the files that had the letters d, a, r, k, e and m got replace with a."  Huh?  Do you mean "Every file whose name contained any of D, a, r, k, e, and m got renamed to the same name, with all of those letters replaced by periods (.); e.g., cat, dog, meerkat, and Dog got renamed to c.t, dog, ......t, and .og, respectively."?  Did any files get clobbered, or is this just a rename problem? Commented Aug 8, 2015 at 22:44
  • 1
    Small trick for future, prepend a critical command with echo so you can verify it does exactly what you expect. Though that might require modification of the original command. Commented Aug 10, 2015 at 3:15

2 Answers 2

5

If you've a snapshot of the filesystem, or a backup, then yes.

If you haven't, then no.

1
  • Well, there is (rather was) a way: copying the output from his verbose cp -v command and swapping the input and output files in a simple script Commented Aug 10, 2015 at 8:40
0

There might be a way also using the locate database and writing a custom script... The database is regularly updated, but usually it is updated once in the day only so you can get a snapshot of the file names if you are fast enough.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.