Tuesday, May 26, 2015

What would happen if we type "mv *" in the command line?

What will happen if we type the command  mv * ?

To check it, I went to folder containing the following entries

$ ls -l
total 24
-rw-rw-r-- 1 xxxx yyyy  97 Mar  2 18:51 elb
-rw-rw-r-- 1 xxxx yyyy  96 Mar  6 12:09 elb2
-rw-rw-r-- 1 xxxx yyyy 108 Mar  6 11:49 elb3
-rw-rw-r-- 1 xxxx yyyy 132 Mar  6 11:53 elb4
-rw-rw-r-- 1 xxxx yyyy 128 Mar  6 11:56 elb5
-rw-rw-r-- 1 xxxx yyyy  10 May 24 16:20 host.txt

$ mv *
mv: target `host.txt' is not a directory

To see what the above command mv * did really, I ran the following command

$ echo mv *
mv elb elb2 elb3 elb4 elb5 host.txt

So the shell basically expands * to all the file names in the directory before calling the mv command.

So the mv command sees the last argument as the destination- in this case it is host.txt. It is a file and not a directory. The reason for mv command to consider the last argument as a directory is specified in the man command for mv

DESCRIPTION
       Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

More info about mv command can be found from

$ info coreutils 'mv invocation'

No comments:

Post a Comment