Ubuntu – Accidentally made a file disappear using mv. Where should I look for it

command linefilesfilesystemhidden-files

I had a file in which I had some credentials; and for some reason, it's hidden now. I can see on my desktop that there are two folders with . and .. name. Where did they come from? My file was sample.txt. I messed up with the mv command, and the file became hidden. Now, I'm trying to figure out where my file is.

I need some help!

Edit: These are the commands I used:

1861  mv sample.txt Downloads
1872  find sample.txt
1887  mv .. sample.txt
1888  mv ... sample.txt

Best Answer

  • The issue here is that the behavior of commands like mv source target, when target does not end with a /, depends on whether there is an existing directory (or an existing symbolic link that points to a directory) called target. Sometimes that makes them do a different thing than what you expect.

    You showed four commands. The first command is the important one:

    mv sample.txt Downloads

    That command moved or renamed sample.txt.

    If there was nothing called sample.txt in the current directory in the first place, then mv sample.txt Downloads failed and no change was made. But based on your description, it sounds like there was such a file. So:

    1. If there was a directory called Downloads in the current directory, then mv sample.txt Downloads tried to move sample.txt into the directory called Downloads, and probably succeeded. This also happens if Downloads is a symbolic link to a directory rather than an actual directory.
    2. If there was no directory called Downloads in the current directory, then mv sample.txt Downloads tried to rename sample.txt to be called Downloads. (Not Downloads.txt, just Downloads.)

    If this was in a Desktop directory, the second scenario is probably what occurred, since you probably don't have a Downloads directory in a Desktop directory. But if you do, you're in the first scenario.

    So you should look inside the directory you were in when you ran mv sample.txt Downloads for an entry called Downloads. Depending on what existed before, this should either be the file formerly known as sample.txt, or it should contain sample.txt.

    As for the other three commands you showed, they should've made no changes at all, and they also should not be expected to have revealed any useful information about the effect of the first command.

    find sample.txt won't show anything unless there's something called sample.txt residing directly in the current directory. If you want to find files contained anywhere, directly or indirectly, in the current directory, whose names are sample.txt, you would use find . -name sample.txt. (Of course, that won't find a file whose name is no longer sample.txt.)

    In mv .. sample.txt, .. is a name for the parent directory, which is the directory that the current directory resides in, or if the current directory is /, then the parent directory is / itself. Every directory has a .. entry with this meaning. That command tries to rename the .. entry for the parent directory to be called sample.txt. This should simply fail with an error, which in this case will somewhat confusingly be reported as "Device or resource busy".

    In mv ... sample.txt, ... is not treated specially. Directories always contain the special entries . and .., but the name ... is not special. So you probably didn't have a file called ....

    • Assuming you didn't, that command failed and made no changes.
    • If you did have a file called ... in the current directory, and no directory called sample.txt (and no symbolic link called sample.txt that points to a directory), then ... would be renamed to sample.txt. If sample.txt already existed and was neither a directory nor a symbolic link to a directory, it would be replaced and (unless there are other hard links to it) it could be difficult or impossible to recover depending on how much the filesystem has subsequently been written. However, this is unlikely, since you probably never had a file called ..., and since that command was run after a command that probably succeeded either at renaming sample.txt in place or putting it somewhere else, and since you don't currently see a file called sample.txt in the directory where you ran the commands.
    • In the unlikely event that you had both an entry in the current directory called ... and another entry sample.txt that was a directory or a symbolic link to a directory, mv ... sample.txt would try to move ... into the sample.txt directory (or into the directory targeted by the symbolic link).
  • Related Question