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

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
  • 11,391