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

末鹿安然 提交于 2019-12-06 02:36:56

问题


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)?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18324993/what-does-an-asterisk-at-the-end-of-a-mv-command-do

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