9

I had a directory with a file and another empty directory in it like this:

.
..
file.ext
folder

I did:

mv file.ext /folder

The file disappeared now, not in folder too not where it was before!

Anyway to find it back?

Thanks

heemayl
  • 91,753
nabtron
  • 411

2 Answers2

6

If the directory /folder (under root directory, /) does not exist, and if you were working as root, you have renamed (moved) the file file.txt as folder under /.

Doing a ls -l /folder should show you the file.

Another point is, if a directory /folder does exist, then look under that directory for the file i.e. /folder/file.txt.

Also note that if you append a / to the destination path, you would get a different message like:

mv: cannot move ‘file.txt’ to ‘/destination/’: Not a directory

and the file will not be renamed.

For example:

% sudo mv file.txt /spamegg/
mv: cannot move ‘file.txt’ to ‘/spamegg/’: Not a directory

% sudo mv file.txt /spamegg 

% ls /spamegg  
/spamegg
heemayl
  • 91,753
  • I have a question, if I move the file somewhere other than / like at /folder/folder2/folder3 it doesn't become folder3, rather it creates a new folder3 if it doesn't exist already and then sits there? or will it do the same and become a file folder3 there too? – nabtron Nov 16 '15 at 03:20
  • @nabtron the rule is universal..if a directory exists the file will be copied into it and if the directory does not exist it will be named as the "directory" i.e. last component.. – heemayl Nov 16 '15 at 03:34
  • Unless you also end the path with a slash, eg /folder/folder2/folder3/... If that doesn't exist you'll see a mv: failed to access ‘...’: Not a directory – Oli Nov 16 '15 at 09:58
3

If you wanted to move the file to the folder in the same directory, you should have done this:

mv file.ext folder

However, because you have done this instead:

mv file.ext /folder

the file has been moved and renamed to /folder instead of folder/file.ext

.

Run the following command:

cd /

and then run:

ls | grep folder

and it should show up in the results.

mchid
  • 43,546
  • 8
  • 97
  • 150