5

I am in a trouble. I pressed 'enter' without the destination. I had two files in the same directory with a common IDENTIFIER in their name. I wanted to move them to a folder, so I entered

mv /path/to/file/IDENTIFIER*

But before I enter destination I pressed 'enter' and one of my file disappeared which was alphabetically higher.. Now I can not find that file anywhere. I was in my

Abhishek@abhishek$ directory in the terminal.

Can anyone give me an elaborate answer about how I can find or is it overwritten by the other file. I am a complete rookie in the Linux world but I know the workhow of the terminal. So I can do some basic commands.

3 Answers 3

7

If you have only two files, eg:

IDENTIFIER_1
IDENTIFIER_2

then you have overwritten IDENTIFIER_2 with the content of IDENTIFIER_1.

Example:

$ cat IDENTIFIER_1
IDENTIFIER_1

$ cat IDENTIFIER_2
IDENTIFIER_2

$ ls -og IDENTIFIER_*
-rw-rw-r-- 1  0 Mai 19 18:28 IDENTIFIER_1
-rw-rw-r-- 1 13 Mai 19 18:27 IDENTIFIER_2

$ mv IDENTIFIER_*

$ ls -og IDENTIFIER_*
-rw-rw-r-- 1 13 Mai 19 18:30 IDENTIFIER_2

$ cat IDENTIFIER_2 
IDENTIFIER_1

If you had had more than two files, then would be an error:

$ ls -og IDENTIFIER_*
-rw-rw-r-- 1  0 Mai 19 18:28 IDENTIFIER_1
-rw-rw-r-- 1 13 Mai 19 18:27 IDENTIFIER_2
-rw-rw-r-- 1  0 Mai 19 18:28 IDENTIFIER_3

$ mv IDENTIFIER_*
mv: target ‘IDENTIFIER_3’ is not a directory

For an even better explanation see @Serg.

3
  • One of the tricks that I learned from years ago, to move the contents into mkdir z; mv * which moves everything into the directory z... of course, assuming that the z directory is the last item in the * expansion.
    – user153310
    May 19, 2015 at 21:28
  • 1
    Sounds dangerous :)
    – A.B.
    May 19, 2015 at 21:30
  • You could always touch a file named -i if you are worried about that.
    – user153310
    May 19, 2015 at 21:33
4

I would like to merely expand on the answer that A.B. posted.

The wildcard merely expands IDENTIFIER_* to all instances of IDENTIFIER_*. Therefore, mv IDENTIFIER_* in reality is read as mv IDENTIFIER_1 IDENTIFIER_2.

This is a same reason why for loops work like so for file in *; do , as well as echo IDENTIFIER_*, and so on and so forth.

Now the reason why 3 files dont work is again because the command mv IDENTIFIER_* expands to mv IDENTIFIER_1 IDENTIFIER_2 IDENTIFIER_3, and you are confusing mv with too many arguments.

enter image description here

2
  • I should delete my answer =) +1
    – A.B.
    May 19, 2015 at 19:30
  • no, no, don't delete it. It's still shows everything correctly May 19, 2015 at 19:31
1

You overwrote the second file with the first one.

It's lost unless you unmount immediately the partition/device and try to recover it with a tool like testdisk, photorec, extundelete, or whatever.

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.