File disappeared on move

22,479

Solution 1

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

Solution 2

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.

Share:
22,479

Related videos on Youtube

nabtron
Author by

nabtron

Code is Poetry.

Updated on September 18, 2022

Comments

  • nabtron
    nabtron over 1 year

    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

    • Admin
      Admin over 8 years
      Were you working as root? if so, you probably just renamed file.ext to /folder (which would be a file in the filesystem root directory /): look there.
    • Admin
      Admin over 8 years
      yes its there, now how to retrieve it? :) ??
    • Admin
      Admin over 8 years
      it doesn't matter if you are working as root or not, because there was a / in front of the folder name, it will go to / which is the root of your file system.
    • Admin
      Admin over 8 years
      no mchid, it wouldn't go there, it would become that steeldriver is right, it became the file with name folder
    • Admin
      Admin over 8 years
      steeldriver please write it as an answer so that I can mark it as correct
    • Admin
      Admin over 8 years
      @nabtron yes it would go there because that is the path
    • Admin
      Admin over 8 years
      well i just checked and it was a file with name "folder" in /
    • Admin
      Admin over 8 years
      can you please explain?
  • nabtron
    nabtron over 8 years
    no it wouldn't go in that folder, it would become a file named folder in / folder
  • mchid
    mchid over 8 years
    @nabtron only if the directory of that name did or does not exist would that happen
  • mchid
    mchid over 8 years
    @nabtron and then, it would become a file named folder in /
  • nabtron
    nabtron over 8 years
    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
    nabtron over 8 years
    does it happen for root only or for all?
  • mchid
    mchid over 8 years
    @nabtron when working as any user
  • nabtron
    nabtron over 8 years
    no i mean for root main directory or for any subdirectory too?
  • mchid
    mchid over 8 years
    @nabtron Yes, you are right for all directories. My answer assumed there was a directory named folder in / already but since there was not I guess the file was renamed /folder.
  • heemayl
    heemayl over 8 years
    @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..
  • nabtron
    nabtron over 8 years
    yap! i appreciate your help!
  • Oli
    Oli over 8 years
    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