difference copy contents folder between /. and /* in linux
There is a fundamental difference between these two argument forms. And it's an important one to understand what is happening.
With ../foldersource/.
the argument is passed unchanged to the command, whether it's cp
or rm
or something else. It's up to the command whether that trailing dot has special or unique semantics different from the standard Unix convention of simply pointing to the directory it resides in; both rm
and cp
seem to treat it as a special case.
With ../foldersource/*
the argument is first expanded by the shell before the command is ever even executed and passed any arguments. Thus, rm
never sees ../foldersource/*
; it sees the expanded version ../foldersource/file1.ext ../foldersource/file2.ext ../foldersource/childfolder1
and so on. This is important because operating systems limit how many arguments can be passed to a command, usually only a few hundred.
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 (.
).
You cannot do rm -rf ../foldersource/.
because rm
doesn't allow this, as explicitly stated in the manual:
Any attempt to remove a file whose last file name component is ‘.’ or ‘..’ is rejected without any prompting, as mandated by POSIX.
and in the POSIX manual man 1p rm
we see:
If either of the files dot or dot-dot are specified as the basename portion of an operand (that is, the final pathname component) or if an operand resolves to the root directory, rm shall write a diagnostic message to standard error and do nothing more with such operands.