2
$ rename 's/./ /g' *

$ ls -l
total 6832616
-rw-r--r-- 1 jboo jboo         0 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  43361681 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  47499946 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  55881060 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  90555631 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  39386207 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 147367671 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  64708219 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  74487434 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 102734315 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  63437787 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 102899063 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 158347478 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  79617708 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  70734588 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  74811527 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 116806547 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 142705146 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 525401910 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  84534720 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  91861219 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 333548107 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  83682222 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 142117082 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 361824861 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 168574047 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  69705069 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  57178994 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 185741017 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 228484548 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 268336818 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 178709785 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 201254796 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 461899931 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 302733730 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 135959214 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 175477963 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 120373878 Sep 28 01:44
-rw-r--r-- 1 jboo jboo  82542816 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 212611146 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 206069847 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 246377910 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 266923675 Sep 28 01:44
-rw-r--r-- 1 jboo jboo 329243380 Sep 28 01:44

As you can see, the files are still there but are now invisible. Anyone have any idea how I can rename them back?

AdminBee
  • 21,637
  • 21
  • 47
  • 71
A H
  • 183
  • 5
  • Not invisible but all spaces, I think. See [Regular expression to match a dot](https://stackoverflow.com/a/13989661/10765659). – Kamil Maciorowski Sep 28 '20 at 09:11
  • 3
    The characters in the names have been replaced by spaces. Files having names with the same length as other files have been overwritten by, or has overwritten, those other files. Restore from backup or recreate the files. – Kusalananda Sep 28 '20 at 09:17
  • Pretty scary (and funny) stuff ! Kamil is right; you just renamed all your files replacing everyone of their name's characters with a blank space. To actually do what you intended, you would have needed to escape the dot, as in `rename 's/\./ /g' *`. The question is why would you want to do that at all ? – Cbhihe Sep 28 '20 at 09:25
  • 1
    Yeah I know I''m kicking myself for not escaping the dot. But what I don't get is what the names actually are. I pulled the original file names from my mlocate database, should I assume the new filenames are number characters long the original filename was, but whitespace? – A H Sep 28 '20 at 09:47
  • 1
    Yes exactly. For the files with same filename length, I guess the last processed from the `*` evaluation has overwritten the rest with same length. – thanasisp Sep 28 '20 at 09:52
  • And renaming them back to something human-readable, even if they all have a different number of spaces, so you can identify the contents, is going to be fun :-) – dirkt Sep 28 '20 at 09:55
  • 1
    `rename 's/ /n/g' *` may simplify further manual renaming a little. – Kamil Maciorowski Sep 28 '20 at 09:59
  • Awesome suggestion Kamil. Thx. – A H Sep 28 '20 at 10:19
  • 3
    I'm surprised it replaced *all* the names - unless every original name had a unique length - since AFAIK the Perl-based `rename` has a default "no-clobber" behavior (ex. if I try to rename both 'foo' and 'bar' it responds `'foo' not renamed: ' ' already exists`) – steeldriver Sep 28 '20 at 11:33

2 Answers2

1

As you have guessed from the comments, all your filenames are now spaces. You may have actually lost a few files in the process.

The first option is to look at your backups. Since you may have lost files, for those files your backup is the real solution. There may be some newer versions of the files available, so do not just overwrite everything. Use Kamil's rename 's/ /n/g' * to compare the new file name. Note that you can only look at the number of characters!

If you did not make backups, the next stop is the file command. It will (probably) tell you what kind of file you have. Then it is a question of using the right viewer to see what the content of the file is. You might need to re-invent the actual name.

But AFAIK, there is no 'undo' for this action.

Ljm Dullaart
  • 4,142
  • 11
  • 26
1

This is not exactly an answer but like a long comment and I post it in case it is helpful.

I could not confirm this behaviour of rename in my shell but probably it is different across shells and distros. I assume you have renamed files with spaces and now you have renamed them with a visible character. Also I see in comments that you managed to get the original filanames from the mlocate db.

The glob evaluation is alphabetical, that means when rename * was executed, for file a and file b, having same filename length, finally the b file was written and a was lost.

Now you could partially restore your directory, not the overwritten files of course, but rename back whatever survived. Maybe this is helpful to have a better view:

Assuming you have the list of the names in a file

> cat files.txt
a
b
c
file1
file2
some_lucky_file

you can sort them by filename length, and secondarily alphatically, and finally remove the duplicates, keeping for every filename length the survivor:

awk '{print length($0), $0}' files.txt | sort -nk1 -rk2 | awk '!seen[$1]++'
15 some_lucky_file
5 file2
1 c

These should be the existing files. At this point you can list them by side with the renamed files and check them. Maybe you have done that already. Good luck.

thanasisp
  • 7,802
  • 2
  • 26
  • 39