2

Here's the problem. I had a bunch of files in a directory. Then I created another directory in that directory. Then I cobbled together this command:

find . -maxdepth 1 -type f -exec mv {} ./1 \;

This command was supposed to take all the files in the directory and move them to that newly-created directory, but instead of providing the name of the directory, I screwed up and typed 1, as you can see from the code snippet. So, I ended up having just one text file named 1 that now contains the stuff from one of the disappeared files and that's all.

Is there any chance I could recover the lost files (or possibly the actual data from the files--they were all text files) or are they pretty much permanently gone?


Before:

misha@hp-laptop:~/Documents/prgmg/work$ ls
add.s  bubble.s  cpuid.s  div.s    hello.s  mult.s       sum.s   test.s
a.out  c         demo.s   gas.txt  max.s    print_arr.s  test.c
misha@hp-laptop:~/Documents/prgmg/work$ mkdir asm

After:

misha@hp-laptop:~/Documents/prgmg/work$ ls
1  asm  c

So, as you can see, I wanted to put all assembly language files into the asm directory. And as things stand now, 1 is a text file and it contains the stuff from gas.txt.

  • I have mistyped directory names many times and moved or copied a file into a filename instead of directory. To avoid this I have begun to always place a trailing slash "/" so that I would get an error that the directory doesn't exist if I mistype the directory name. Or at worst, it will go into a wrong directory. – L. D. James Jul 14 '16 at 23:24
1

You have lost ALL but the last file offered up by find, it is now called ./1. Note that find finds files in its own order, so "last" doesn't mean what you think. By inserting an echo in front of the mv, we see:

Script started on Thu 14 Jul 2016 07:02:01 PM EDT
+w3@aardvark:~(0)$ mkdir tmp/answer
+w3@aardvark:~(0)$ cd !$
cd tmp/answer
+w3@aardvark:~/tmp/answer(0)$ for i in a b c d; do
> mkdir $i
> done
+w3@aardvark:~/tmp/answer(0)$ for i in a/1 b/2 c/3 d/4 a/0 ; do
> touch $i
> done
+w3@aardvark:~/tmp/answer(0)$ touch 9 8 7 6
+w3@aardvark:~/tmp/answer(0)$ find . -maxdepth 1 -type f -exec echo mv {} ./1 \;
mv ./8 ./1
mv ./6 ./1
mv ./9 ./1
mv ./7 ./1
+w3@aardvark:~/tmp/answer(0)$ exit

Script done on Thu 14 Jul 2016 07:05:21 PM EDT

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.