when I use * in cp, I think it follows the same rule as regex.
so "cp temp/* test/" should copies everything over, however, when temp folder is empty it throws exception saying it cannot find file or directory, which indicte * cannot match “nothing”.
Then I create a file test.txt under temp and do:
cp temp/test.txt* test/
It works, which indicate * indeed match “nothing”.
I get confused about the behavior. Can anyone explain a little bit?
Thanks
What’s happening is the
*expansion is done by your shell (bashprobably). The patterntemp/testfile.txt*did matchtemp/testfile.txt(*matches zero or more characters), so bash passed that ontocp.However,
bashis set, by default, to pass the wildcard as-is on to the app if it doesn’t match anything (there’s an option callednullglobto turn this non-intuitive behavior off). So it passedtemp/*literally to cp, which complained that it didn’t exist.