3

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/lib

My 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)

2
  • 1
    The behavior you assumed is the actual one, both files should have been move to /u/projects/lib. Sometime it helps to try echo {command...} first to see what file expansion does before running the command. Btw, your target directory name is unusual. Are you on native Ubuntu or is this WSL or else?
    – xenoid
    Commented Aug 2, 2019 at 13:50
  • I am on native Ubuntu Commented Aug 5, 2019 at 8:43

1 Answer 1

12

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
  • 1
    ah yes. :=) fell for that again
    – Rinzwind
    Commented Aug 2, 2019 at 14:50
  • @mook765 nope it will error out.
    – Rinzwind
    Commented Aug 2, 2019 at 15:17
  • The file names were sy_lib.4gl, and sy_lib_overd.4fd. I know that both files existed in both directories and that the lib directory existed when I moved them but for some reason the sy_lib.4gl file was not moved. Only sy_lib_overd.4fd was moved correctly and sy_lib.4gl was nowhere to be found. Commented Aug 5, 2019 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .