I ran the mv command to move two files, let's call them file_name_1, file_name_2. The command I used was:
mv file_name* /u/projects/libMy assumption was that this would move both files into the lib directory. What ended up happening was that file_name_2 got moved correctly but file_name_1 was deleted from the directory I was moving it from and the directory I was moving it to.
Losing the file wasn't a big deal as I was able to recover the file from a backup and did not lose any work but I am curious as to why this would happen and what would be the best way to prevent it from happening again? (For now I'm just copying the files over and then deleting the source file once I am sure it copied over fine)
21 Answer
mv Command Deleted Files In Source Directory and Target Directory
-Impossible-. That is NOT how mv works.
- "mv" does a "cp -a" if it actually moves files (as in across filesystems) and only if it gets a confirmation the "cp -a" was successful it does a remove. See $ info mv for a detailed explanation on this.
Moving files within the same file system uses the rename call and that is just a metadata change. The file itself is not touched.
mv file_name* /u/projects/lib
"file_name*" was likely 1 file and "lib" did not exist. As a result you renamed "file_name*" to "lib" by omitting the "/" at the end of "/u/projects/lib". If you had added the "/" the command would have shown an error that the directory "lib" did not exist. A file rename that does not cross file system boundaries is just a metadata change.
Had "file_name*" been 2 or more files you would get an error about moving files onto the same file. And had "lib" existed as a directory you'd find the files in "lib/"
3