Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to understand the significance of the dot in bash and how it differs from an asterisk. Can someone please elaborate? For example, what's the difference between cp -ar /foo/. /foo2/ and cp -pr /foo/* /foo2/

share|improve this question

1 Answer 1

up vote 7 down vote accepted

The dot (.) is just the . directory inside a directory which is the same as the directory itself (ls -la /foo will show it to you). So if you copy foo/. elsewhere, you effectively copy all the tree below foo without copying foo itself (you're copying it into foo2/. which is the same as foo2).

/foo/* is expanded by the shell to the list of non-hidden files and directories in /foo, so it would copy the same directory structure under /foo into /foo2 as well, except for the dotfiles/dotdirs. Effectively, cp will receive (potentially) many arguments. If the list is too big, it may even cause the execution of cp to fail.

share|improve this answer
    
Sorry but I'm getting confused. So by nature, the asterisk makes the command recursive? In other words cp -r /foo/* does the same thing as cp /foo/* ? –  Mike B Jan 21 '13 at 21:28
3  
@MikeB, no, the shell only expands the list of files in the /foo directory. It doesn't alter the behavior of cp. Use set -x or echo cp -pr /foo/* /foo2 to see what's going on. It's important to realise that it's the shell that expands the wildcards. cp doesn't see the star. –  Stéphane Chazelas Jan 21 '13 at 21:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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