19

What is the difference between following 2 commands?

cp -rp /dir1/*.* /dir2/
cp -rp /dir1/* /dir2/
11
  • 12
    Even though both answers are correct, I think there is one thing to be noted. Hidden files (files that start with . like .bashrc) are not copied with any of those commands. To copy those files you should explicitly indicate that with .* so, to copy all files (including hidden ones) the command would be cp -rp /dir1/.* /dir1/* /dir2/
    – YoMismo
    Sep 29, 2015 at 6:52
  • In short, *.* is the MS-DOS/Windows way of doing it, while * is the UNIX/Linux way. In Unix suffix (file-ending, the part after the . - like .txt or .jpg) is optional... In MS-DOS the . (dot) and suffix was required, so to match all files, one used *.* - while to match all text-files, one would use *.txt. Sep 29, 2015 at 9:34
  • @BaardKopperud suffix wasn't required, there could be files with name like FILENAME or even FILE.
    – Ruslan
    Sep 29, 2015 at 11:57
  • 1
    @Ruslan but *.* in DOS still matches files named FILENAME or FILE, because the name is 'really' FILENAME. or FILE. (with an empty extension). In Unix, the name is 'really' FILENAME or FILE, so *.* won't match. Sep 29, 2015 at 16:01
  • 1
    @Ruslan It was required on actual DOS, not required anymore on windows. Back in the old days, a single * would only match FILE, and not FILE.TXT.
    – Random832
    Sep 29, 2015 at 16:54

2 Answers 2

25

*.* only matches filenames with a dot in the middle or at the end. For example:

abc.jpg
def. 

* matches the filenames above, plus the names which don't have a dot at all. for example:

data
7
  • 6
    Maybe also note that the anti-idiom *.* used to be the correct way to match all files on MS-DOS back in the day of 8+3 filenames. In MS-DOS, the dot is mandatory (albeit implicit on files which do not have an extension).
    – tripleee
    Sep 29, 2015 at 6:44
  • 11
    @Mark, No, I don't think so. unless you have dotglob on. Sep 29, 2015 at 7:32
  • 11
    Huh, I never realized that *.* matches foo. but not .foo. That asymmetry is terrible.
    – jamesdlin
    Sep 29, 2015 at 7:42
  • 9
    @jamesdlin, from bash manual: When a pattern is used for filename expansion, the character ‘.’ at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set. Sep 29, 2015 at 7:52
  • 1
    @jamesdlin. Since the 1st day of Unix (circa 1969), directory entries starting with a dot were considered hidden. Originally this was needed to skip the . (current directory) and .. (parent directory) entries which always exist in any directory (even empty dirs). Ritchie and Thompson later considered it to be a useful feature to hide (by default) all the .*rc config files and added an explicit -a option to ls to display all these (leading-dot) "hidden" entries. So this is not very surprising knowing Unix history. A leading dot in Unix has been special since "forever".
    – arielf
    Oct 5, 2015 at 22:07
12

Suppose your have following files in /dir1:

foobar  
foo.bar  
fo.ba  
foo1.bar2  
foobar1  
foobar2  

cp -rp /dir1/*.* /dir2/

This command will copy only the following files:

foo.bar  
fo.ba  
foo1.bar2 

cp -rp /dir1/* /dir2/

This will copy all the files in /dir1

The condition applies to the subdirectories in /dir1 as well..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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