2

I executed the following command, intending to move the file into a subdirectory.

sudo mv myFile.jar hexara/*

Should I have used the following syntax instead?

sudo mv myFile.jar hexara

or?

sudo mv myFile.jar hexara/

Sudo is being invoked because I am in an area where the account does not have write privileges. I'm using SSH to work in a remote server.

In any event, after this, an ls of the subfolder hexara shows that the file I moved is not there and that many files in the hexara directory are now missing. Previously there were three subfolders and an index.htm file, but now only one subfolder remains.

Did my malformed mv command cause the files to disappear, or should I suspect some other form of glitch or a different error was made? There were intervening commands--I navigated to another area and returned before cd'ing into hexara and discovering the loss, and made a copy of the CLI if that is of any help.

Is something like this recoverable?

An ls -al showed the following:

phil@ladonax:/var/lib/jetty9/webapps/hexara$ ls -al
total 12
drwxr-xr-x 3 jetty adm 4096 Jun 30 10:26 .
drwxr-xr-x 6 jetty adm 4096 Jun 30 10:26 ..
drwxr-xr-x 4 jetty adm 4096 Jun 30 10:26 VSL

VSL, by the way, has 14 files in it, so I don't know where the above "total 12" comes from. The hexara directory might have had three subfolders and only one file in it before the loss. But I don't see why this would have turned up in the "total" but not appear in the accompanying listing.

Fortunately I have backup files of this directory. But I'm really puzzled as to what happened. Am using Ubuntu 20.04.

Yes, there is a task on the queue to upgrade Jetty but I assume that would not have any pertinence to this question.

EDIT: I discovered that all the files in the hexara subdirectory were moved into hexara/VSL. Any thoughts as to why ../webapps $ mv myFile.jar hexara/* would cause files in ../webapps/hexara to be moved into ../webapps/hexara/VSL?

8
  • 1
    When the last arg of mv is a dir, all the previous args (files) get moved there. You can simply move them back up a level (mv file .. ).
    – ubfan1
    Jun 30 at 18:37
  • If you're not sure about mv usage, it takes only seconds to create a couple empty testfiles and test directories to verify that your syntax does what you expect. Be sure before using sudo. "Hmmm...let's make sure first" is the motto of a true professional.
    – user535733
    Jun 30 at 18:51
  • @user535733 my method is to do an ls since that will show all files the mv will move :)
    – Rinzwind
    Jun 30 at 18:56
  • mv is weird. I think I am going to go back to using cp and deleting the original after verifying the copy was made correctly. Jun 30 at 18:56
  • Ah, learned that this was due to an unexpected * expansion. Maybe mv isn't as weird as I thought. Jun 30 at 19:13

1 Answer 1

5
sudo mv myFile.jar hexara/*

this will move myFile.jar into ./hexara/VSL/. Proof:

rinzwind@discworld:~/tmp/tmp/tmp$ ls
hexara  myFile.jar
rinzwind@discworld:~/tmp/tmp/tmp$ pwd
/home/rinzwind/tmp/tmp/tmp
rinzwind@discworld:~/tmp/tmp/tmp$ mv myFile.jar hexara/*
rinzwind@discworld:~/tmp/tmp/tmp$ ls hexara/VSL/
myFile.jar

To revert do

cd hexara/VSL/
mv myFile.jar ../../

Or to fix (since you want it in ./hexara/)

cd hexara/VSL/
mv myFile.jar ../

The * gets expanded so when there are 2 or more directories everything moves to the last directory.

mv myFile.jar hexara/*

does

mv myFile.jar hexara/VSL hexara/WSL 

Proof:

rinzwind@discworld:~/tmp/tmp/tmp$ ls -ltr
total 4
-rw-rw-r-- 1 rinzwind rinzwind    0 jun 30 20:45 myFile.jar
drwxrwxr-x 4 rinzwind rinzwind 4096 jun 30 20:51 hexara
rinzwind@discworld:~/tmp/tmp/tmp$ ls -ltr hexara/
total 8
drwxrwxr-x 2 rinzwind rinzwind 4096 jun 30 20:51 WSL
drwxrwxr-x 2 rinzwind rinzwind 4096 jun 30 20:51 VSL

and after the move ...

rinzwind@discworld:~/tmp/tmp/tmp$ mv myFile.jar hexara/*

VSL is moved too:

rinzwind@discworld:~/tmp/tmp/tmp$ ls -ltr hexara/
total 4
drwxrwxr-x 3 rinzwind rinzwind 4096 jun 30 20:52 WSL
rinzwind@discworld:~/tmp/tmp/tmp$ ls -ltr hexara/WSL/
total 4
-rw-rw-r-- 1 rinzwind rinzwind    0 jun 30 20:45 myFile.jar
drwxrwxr-x 2 rinzwind rinzwind 4096 jun 30 20:51 VSL

requiring more repairs.

5
  • I was inspecting my CLI record more closely and just noticed that everything that was in the hexara folder was moved into VSL. This makes no sense to me, but it matches what you describe in your answer. If I am in /tmp and execute mv myFile.jar hexara/*, why would the contents of /tmp/hexara all be moved into /tmp/hexara/VSL? Jun 30 at 18:53
  • 2
    @PhilFreihofner the * will get expanded to mv myFile.jar hexara/VSL hexara/WSL so all files end up in the last directory.
    – Rinzwind
    Jun 30 at 18:58
  • I see. OK that makes really good sense. Thank you so much. Wish I could upvote twice! Jun 30 at 19:03
  • 1
    Without wishing to sound patronising, it is a good illustration of why you have to be careful with using wildcards that are expanded by the shell - the shell sometimes expands things in unexpected ways if you're not expert ... I avoid using wildcards unless I'm very sure it will do what I want. Nice question and answer, got me experimenting on the CLI to reproduce it and learn something - thank you both!
    – Will
    Jun 30 at 19:04
  • Thank you Will. Very diplomatically put, too. Jun 30 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.