Thursday, September 24, 2020

bash - copy files from one directory into an existing directory

 Original post: https://stackoverflow.com/questions/3643848

 cp -R t1/. t2/

 The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.

 
If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!