views:

673

answers:

7

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?

+2  A: 

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.

Greg Hewgill
A: 

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
Wayne
Ok, my bad assumption was that $something_that_does_not_exist was something you would type, not a shell variable. As others have said, this will fail
Wayne
I think is a joke question, so mv files* /dev/null is probably the better response :)
Wayne
+2  A: 

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.

hlovdal
You are on the right track. The event is related to the last command. I reasoned wrong that the first thing would occur as I noticed the last thing. I am still rather confused WHY the thing happens.
Masi
A: 

What system/shell are you running on. I tried the commands above under unbuntu linux, it just gave me errors.

$ mkdir folder 
$ mv folder $something_that_does_not_exist 
$ mv: missing destination file operand after `folder' 
      Try `mv --help' for more information

$ touch files1 files2 files3 
$ mv files* $something_that_does_not_exist 
$ mv: target `files3' is not a directory

Of course if $something_that_does_not_exist is defined then, it will move your files to whatever the value of $something_that_does_not_exist is. You can find the value of $something_that_does_not_exist by typing:

$ $something_that_does_not_exist

If that command returns nothing, then $something_that_does_not_exist it is undefined. If that command prints a value, that value is where you moved the files to.

I'm thinking this may be a joke question. Can others confirm this actually happens?

e5
I was using Zsh/Bash on Mac. I am getting more precise info asap.
Masi
Please, see the answer of the user hlovdal.
Masi
A: 
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.

Jon Ericson
A: 

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.

sth
A: 

Hi,

I'm also facing the same problem. I accidentally moved several files into a non-existence files and I forgot to typed the slash (/) in front of the something_that_does_not_exist

find . -name "filename*" -exec mv {} something_that_does_not_exist \;

I found that the "something_that_does_not_exist" turned to hidden file and all my filename* are missing.

how can I get all my filename* back?