Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an asterisk at the end of a mv command do

So I am going along and moving a bunch of files

mv /source /dest &
mv /source/* /dest/dest/ &
...
...

then I get careless and

mv /source/filena* /dest/dest/ *

OMG! ^c^c^c^c [No Response from terminal command] What is actually going on here?

What happens when I put an * (asterisk) at the end of a command instead of an & (ampersand)?

like image 530
EchoBinary Avatar asked Aug 20 '13 00:08

EchoBinary


2 Answers

The shell expands the wildcard *. The mv command never sees the wildcard, only the result of the expansion.

The wildcard * expands to the list of files in the current directory in lexicographic order. If the last file is a directory, then all the preceding files (/source.filenafoo, /source/filenabar, /dest/dest, hello) are moved to that subdirectory. If the last file is not a directory, mv complains that “target a.png is not a directory” (or words to that effect).

See What does mv ./* without specifying destination do? for more detailed examples.

like image 57
Gilles 'SO- stop being evil' Avatar answered Oct 26 '22 10:10

Gilles 'SO- stop being evil'


An asterisk at the end of a command line is treated the same way as an asterisk anywhere else on the line — it's a wildcard that matches zero or more characters. Specifically, in this instance, the * in mv /source/filena* /dest/dest/ * is replaced by the name of each & every file and folder in your current directory (except those beginning with a dot), and whatever happens to be last in this list is where mv is going to try to put everything.

like image 2
jwodder Avatar answered Oct 26 '22 08:10

jwodder