When you copy using cp -a source/* target/
you are copying most of the files and directories from source
to target
. Specifically, the items that are excluded will probably be files beginning with a dot (.
) in the top level of source
.
Consider these files (or directories) in source
apple # will be copied
banana/ # will be copied, as will all its contents
.cherry # will not be copied
When you copy using cp -a source/. target/
you are copying the entire contents of source
, including any items beginning with a dot (.
) to target
Consider these files (or directories) in source
apple # will be copied
banana/ # will be copied, as will all its contents
.cherry # will be copied
If you're using bash
, zsh
, you can use the dotglob
option to change the meaning of *
so that it includes files and directories beginning with a dot (yash
also has a dotglob
option; however, it then includes .
and ..
in glob expansions which limits its usability. See also FIGNORE='@(.|..)'
in ksh93
).
Interestingly, cp -a source/. target/
is guaranteed never to create the component target/source
. (On the other hand, cp -a source target/
will do one of two things depending on whether or not target
already exists. See How to copy a folder recursively in an idempotent way using cp for the details.)
When you delete using rm -rf source/*
you are deleting the files and directories within source
that do not begin with a dot (.
). Subject to the dotglob
setting I've already mentioned. It will not delete the directory source
itself.
When you try to delete using rm -rf source/.
it will fail - as others have already explained - because POSIX prohibits deletion of a path whose last component is .
or ..
. The closest equivalent is rm -rf source
, which will delete the source
directory and all its contents regardless of whether or not they begin with a dot (.
).
It's up to the command whether that trailing dot makes any sense
Um, the dot always links to the containing directory itself, so../foldersource/.
shouldn't be any different from stating./foldersource/
and in fact its just a redundant way to state the same thing. No command tries to "make sense" of dot or double dot, since these are standard on Linux/Unix filesystems.rm
simply rejects removing current working directory asjimmij
stated in their answer. Also, note that you are using./
in all of your examples while OP has../
there. Somewhat of a (big) difference. – Sergiy Kolodyazhnyy Jul 24 '17 at 00:45cp -rfva ../foldersource/. ./
is NOT equivalent tocp -rfva ../foldersource ./
. The trailing/.
is significant – Chris Davies Jul 24 '17 at 06:39.
is a standard convention in Unix to denote the current directly. Also, thank you for pointing out that I had used./
in my examples when I should have used../
; I will fix that. You are incorrect about about whether commands "make sense" of the trailing dot; as the OP points out (and I have experienced withcp
) bothrm
andcp
behave differently when a trailing.
is present. I should have written something like "it's up to the command if the trailing dot has special or unique semantics". I'll change my answer for clarity. – eestrada Jul 24 '17 at 23:57