2

All my .c source files and a Readme file were in a subdirectory called assignment2 in a directory called Assignment2. Originally, from assignment2 directory:

$ ls
corrupt.c  decode.c  encode.c  fix.c  Readme

Then I tried to move those files from assignment2 directory to the parent directory Assignment2. I typed:

$ mv endocode.c Assignment2 
$ ls
Assignment2  corrupt.c  decode.c  fix.c  Readme

$ mv decode.c Assignment2
$ ls 
Assignment2  corrupt.c  fix.c  Readme

$ mv corrupt.c Assignment2
$ ls
Assignment2  fix.c  Readme

$ mv fix.c Assignment2
$ mv Readme Assignment2
$ ls
Assignment2

Now my file originally called Readme is called Assignment2 and I can't find my .c source files.

Where did my files go?

Cid
  • 21
  • 2
  • The mv command does not "move" the contents of a file (as you assumed), but rather renames that file because you did not properly specify a directory. There is no Assignment2 directory in the assignment2 directory. You would have to specify .. (or an absolute path) to refer to the parent directory. Except for the renamed Readme, your other files have been deleted. – sawdust Oct 04 '19 at 23:32
  • When moving files to a directory, always specify a slash at the end of the directory name: mv -vi endocode.c Assignment2/ That command will fail if Assignment2 is not a directory. – Jim L. Oct 04 '19 at 23:40
  • "Where did my files go?" (the title) and "how to recover them?" are two separate questions. I removed the latter because (1) it is answered here (it may not be the only duplicate); (2) we strongly prefer one question per question; (3) the existing answer concentrates on the title question. – Kamil Maciorowski Oct 05 '19 at 07:23

1 Answers1

2

As others have stated, they are gone, unfortunately. The mv command rewrote the source with the intended destination.

As Jim said, remember to add the / at the end of the command, the command will fail if the target is not a directory.

However, that is still not exactly fool proof, since it relies on you remembering to add the / at the end of the command.

You can prevent this from happening again, by making an alias.

Create an alias for the mv command

  • With this solution, you'll utilize the -b or --backup arguments for the mv command.

  • alias mv=mv -b will create a backup of the destination file. So in your case, you would have a bunch of backup Assignment2 files.

  • alias mv=mv --backup=numbered this is like the command above, but gives you a bit more control on how you want the backup to be named. I've honestly never used it like this so I don't know what the output would be.

  • FOR MAC USERS the -b flag is not supported on macOS. I don't know for the reason why. If you want to protect yourself, you have to use the -n flag. This tells the command to not overwrite any existing file. alias mv=mv -n

EXTRAS: There are other more advanced options in How do undo MV or RM command? I've never used any of the methods listed in here, besides the -b flag that is explained. I will try to read through them, test them and add them to the answer later on if they work correctly.

DrZoo
  • 10,721