11

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/

11

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.

  • 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
  • 5
    @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
  • What about this? chmod 755 -R . versus chmod 755 -R * – wayofthefuture Feb 23 '17 at 13:54

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.