During the course of some testing, I ran the bast script

for file in *; do
    mv -v "$file" "$file#_*"

or something along those lines. The plan was to remove a prefix from directory names but I accidentally ran it in my home directory rather than in the test directory I had created...

My understanding is that all of the files inside directories should be "safe" (as in, not deleted), but that I now have no way to access them via a directory structure.

Is there a way to recover these files?

Thanks in advance.

UPDATE It appears my memory was incorrect: the command in the for loop was actually echo mv .... Somehow, all the directories and file in ~ were moved to my Videos directory, but appear to be intact.

  • what is the filesystem? – mikeserv Jul 29 '14 at 20:09
  • It's a standard Linux Mint install, so its ext3 I believe. – Nick Jul 29 '14 at 20:28
  • Were the (non-hidden) files all renamed with #_* appended to their name? – Stéphane Chazelas Jul 29 '14 at 20:36
  • @StéphaneChazelas - no. Turns out they got moved them into ~/Videos - there's a comment thread on it on my answer. – mikeserv Jul 29 '14 at 20:54
  • There's no such thing as “not deleted [but] no way to access them via a directory structure”. If the files aren't deleted, there's a way to access them — under their new name. Look at the script that you ran to see what these new names are. – Gilles 'SO- stop being evil' Jul 29 '14 at 22:47

I'm not totally sure what you did there, but if the command in your question is the one you ran, you should just be able to reverse it:

for f in *
do echo mv -v "$f" "${f%"#_*"}"

Please don't remove the echo from that command until you're sure it works, but, just to explain why I think it will, here's what I did:

for f in $(seq 100)
do echo $f > file$f

That made me 100 files like file1 - file100 and each one contained its number, so:

$ cat file100

Like that. Then I did your thing:

for f in *
do mv -v "$f" "$f#_*"

And I had a bunch of files like file1#_* - file100#_*. Then I did my thing pasted in the first codeblock here, but without the echo and I did...

$ cat file100
  • I think the problem with this is that when I do a ls -a, there are no files listed (beyond the ones with a . prefix, which were spared). – Nick Jul 29 '14 at 20:27
  • @Nick - then that is not the command you ran. What is? and what is the filesystem? And really - 0 files that don't begin with a .? Not just 1 file? – mikeserv Jul 29 '14 at 20:28
  • here's the copy paste of the code: for file in * ; do echo mv -v "$file" "${file#*_}" done Apologies for poor formatting, I've never used the stack exchange before – Nick Jul 29 '14 at 20:30
  • @Nick - don't worry about the formatting. Please - what is the filesystem? That's the copy-paste? I think you're in luck! All you did was an echo, if that's it. That command is nerfed. So where did your files go? You should try to remember a name of at least one of them and do sudo find / -name PUTTHATNAMEHERE – mikeserv Jul 29 '14 at 20:32
  • 1
    Thanks a ton for your help! I think everything has survived okay! – Nick Jul 29 '14 at 21:06

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.