cp Linux Commands
What is Linux cp Command?
Explanation
cp COMMAND:cp command copy files from one location to another. If the destination is an existing file, then the file is overwritten;
if the destination is an existing directory, the file is copied into the directory (the directory is not overwritten).
SYNTAX :
cp [OPTIONS]... SOURCE DEST
cp [OPTIONS]... SOURCE... DIRECTORY
cp [OPTIONS]... --target-directory=DIRECTORY SOURCE...
OPTIONS:
-a |
same as -dpR. |
--backup[=CONTROL] |
make a backup of each existing destination file |
-b |
like --backup but does not accept an argument. |
-f |
if an existing destination file cannot be opened, remove it and try again. |
-p |
same as --preserve=mode,ownership,timestamps. |
--preserve[=ATTR_LIST] |
preserve the specified attributes (default: mode,ownership,timestamps) and security contexts,
if possible additional attributes: links, all. |
--no-preserve=ATTR_LIST |
don't preserve the specified attribute. |
--parents |
append source path to DIRECTORY. |
EXAMPLE:
- Copy two files:
cpfile1file2
The above cp command copies the content of file1.php to file2.php.
- To backup the copied file:
cp-bfile1.phpfile2.php
Backup of file1.php will be created with '~' symbol as file2.php~.
- Copy folder and subfolders:
cp-Rscriptsscripts1
The above cp command copy the folder and subfolders from scripts to scripts1.