Copy Contents of Folder in Linux – Difference Between /. and /*

linuxshell

to copy contents from a folder i've read , the use is:

cp -rfva ../foldersource/. ./

but this works too

cp -rfva ../foldersource/* ./

is there any difference?

by example if i want to delete a content from a folder with . :

rm -rf ../foldersource/.

i get the error:

rm:  rejet delete folder '.' or '..':

but with asterisk is ok

rm -rf ../foldersource/*

so, asterisk is better options that works anywhere ?

Best Answer

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.

Related Question