Linux Command - cp

cp - copy files and folders.

Syntax

1
cp [OPTION] SOURCE DEST

or

1
cp [OPTION] SOURCE... DIRECTORY

Description

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Option Description
-f, –force do not prompt before overwriting. Be careful when using this option
-i, –interactive prompt before overwrite
-n, –no-clobber do not override existing file
-s, –symbolic-link make symbolic links instead of copying
-v, –verbose explain what is being done
-R, -r, –recursive copy directories recursively
-a, –archive same as -dR –preserve=all
-p same as –preserve=mode,ownership,timestamps

Example

Copy a file

1
cp -v log1 log2

if target file already exist, then existing file will be overrided. add -i option to prompt before overriding.

Copy a file into a directory

1
cp -v log1 dir

if target file already exist in the directory, the existing file will be overrided. add -i option to prompt before overriding.

Copy a file and preserve mode, ownership and timestamps

1
cp -pv log1 dir

Copy a directory into another directory

1
cp -rv dir1 dir2

dir1 will be copied into dir2. if dir2 not exist, create it.

Copy files/directories in a directory into another directory

1
cp -rv dir1/. dir2

Note the difference between cp -rv dir1 dir2 and cp -rv dir1/. dir2. If you want to copy diles/directories in a directory, use /. at the end of the directory.

Reference