Moving files and folders randomly to nonexistent places

I often execute a command, for example:

mv folder $something_that_does_not_exist mv files* $something_that_does_not_exist 

Then I understand that my files are gone. I don’t even see them in the $ something_that_does_not_exist folder. Where did my files and folders go? How can I get them back?

+4
source share
5 answers

To prevent this problem, I have the habit of always adding / to the end of directory names when using cp or mv :

 $ touch foo $ mv foo bar/ mv: cannot move `foo' to `bar/foo': No such file or directory 

Without a trailing slash, mv performs a file rename operation. You may find that your file has changed name.

+7
source

If $something_that_does_not_exist expands to zero (but I'm not sure if this is your problem?), Then the first mv will fail. The second mv command will also fail if "files*" does not expand to two files, or if the last file name "files*" extends to what will be a directory. Then the files will be moved to this directory.

If the command is in a script and you want your script to interrupt when you try to expand this variable and not be set, you can use the question mark modifier. Example:

 $ echo ${DISPLAY?}; echo display :0.0 display $ echo ${MYTEST?}; echo mytest bash: MYTEST: parameter null or not set $ 

So if you use

 mv folder ${something_that_does_not_exist?} 

and something_that_does_not_exist not set, subsequent commands will not be executed.

+2
source

You will not be able to return your files, since the shell will expand your files, and I believe that the last file in the list will now be called $ something_that_does_not_exist.

All other files in the list will be overwritten. Therefore, you cannot return them.

EDIT: On my virtual virtual machine (the only GNU / Linux on my fingers right now!) I get the following:

 $ mkdir t1 $ mv t1 t2 $ ls t2/ $ mv t2 t1 $ cd t1 $ touch f1 f2 f3 f4 $ mv f* ../t2 mv: target `../t2' is not a directory 
0
source
 mv folder $something_that_does_not_exist 

This should be a mistake:

 $ mkdir folder $ mv folder mv: Insufficient arguments (1) Usage: mv [-f] [-i] f1 f2 mv [-f] [-i] f1 ... fn d1 mv [-f] [-i] d1 d2 

Another case depends on what files* matches:

 mv files* $something_that_does_not_exist 

If the final match is a directory, you will most likely find your files there. Otherwise, you either renamed the first file as the same as the second, or you would have another error, as indicated above.

0
source

1) You are trying to move the Directory folder:

 mv folder abcde 

If "abcde" is an existing directory, it will move the "folder" to "abcde". If "abcde" is an existing file, the command will fail. Otherwise, it will rename the "folder" to "abcde".

2) . You are trying to move some files:

 mv files* abcde 

If "abcde" is an existing directory, it will move the "files *" to "abcde". Otherwise, if there is only one file matching "files *", it will rename this file to "abcde". Otherwise, the command will not be executed.

0
source

All Articles