Moving Files and Folders Accidentally to Non-existent Places

本小妞迷上赌 提交于 2019-12-07 04:26:42

问题


I often do the command such as:

mv folder $something_that_does_not_exist
mv files* $something_that_does_not_exist

Then, I realise that my files are gone. I cannot see them even in the folder "$something_that_does_not_exist". Where have my files and folders gone? How can I get them back?


回答1:


In order to prevent this problem, I have a habit of always appending a / 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 the trailing slash, mv does a file rename operation. You may find that your file(s) have changed name.




回答2:


If $something_that_does_not_exist expands to nothing (but I am not sure if that is your problem?) then the first mv will fail. The second mv command will also fail unless "files*" expand to exactly two files or if the last file name "files*" expands to happens to be a directory. Then the files will be moved to that directory.

If the command are in a script and you want your script to abort when trying to expand the variable and it is not 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 is not set, commands following will not be executed.




回答3:


You will not be able to get your files back, as the shell will expand your files and I believe the last file on the list will now be called $something_that_does_not_exist.

All your other files in the list will have been overwritten. So you cannot get them back.

EDIT: On my smoothwall VM (the only GNU/Linux at my fingers right now!) I get this:

$ 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



回答4:


mv folder $something_that_does_not_exist

This ought to be an error:

$ 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

The other case depends on what files* matched:

mv files* $something_that_does_not_exist

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




回答5:


1) You try to move the directory "folder":

mv folder abcde

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

2) You try to move some files:

mv files* abcde

If "abcde" is an existing directory, it will move the "files*" into "abcde". Otherwise, if there is only one file matching "files*", it will rename that file to "abcde". Otherwise the command will fail.



来源:https://stackoverflow.com/questions/809761/moving-files-and-folders-accidentally-to-non-existent-places

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!