The cp
command stands for “copy.” It is used to copy files and directories from one location to another in the file system. The cp
command can also be used to copy multiple files at once or to copy entire directory structures.
Initial Example
Using cp
to copy a file called source.txt
to a new file called destination.txt
:
UNIX
x
cp source.txt destination.txt
CP Parameters
Parameter | Description |
---|---|
-a, --archive | Copy files and directories recursively, preserving all attributes |
--attributes-only | Copy only the file attributes, not the actual data |
--backup[=CONTROL] | Create a backup of each existing destination file |
-b | Similar to --backup but without accepting an argument |
--copy-contents | Copy the contents of special files recursively |
-d | Same as --no-dereference and --preserve=links |
--debug | Show detailed information about the copying process |
-f, --force | Force the removal of the destination file if it cannot be opened |
-i, --interactive | Prompt for confirmation before overwriting existing files |
-H | Follow command-line symbolic links in the source |
-l, --link | Create hard links instead of copying files |
-L, --dereference | Always follow symbolic links in the source |
-n, --no-clobber | Do not overwrite existing files (deprecated, use --update instead) |
-P, --no-dereference | Never follow symbolic links in the source |
-p | Preserve file attributes like mode, ownership, and timestamps |
--preserve[=ATTR_LIST] | Preserve the specified attributes during the copy |
--no-preserve=ATTR_LIST | Do not preserve the specified attributes during the copy |
--parents | Copy the source file along with its directory structure |
-R, -r, --recursive | Copy directories and their contents recursively |
--reflink[=WHEN] | Control the creation of CoW (copy-on-write) copies |
--remove-destination | Remove each existing destination file before copying |
--sparse=WHEN | Control the creation of sparse files |
--strip-trailing-slashes | Remove trailing slashes from each source argument |
-s, --symbolic-link | Create symbolic links instead of copying files |
-S, --suffix=SUFFIX | Use the specified suffix for backup files |
-t, --target-directory=DIRECTORY | Copy all source arguments into the specified directory |
-T, --no-target-directory | Treat the destination as a normal file, not a directory |
--update[=UPDATE] | Update only when the source file is newer than the destination or the destination is missing |
-u | Equivalent to --update=older |
-v, --verbose | Provide detailed information about the copy operation |
--keep-directory-symlink | Preserve existing symbolic links to directories |
-x, --one-file-system | Stay within the same file system |
-Z | Set SELinux security context of destination file to default type |
--context[=CTX] | Set the SELinux or SMACK security context to CTX, or use the default if CTX is not specified |
--help | Show help information and exit |
--version | Display version information and exit |
Examples
1. Copy a Single File
To copy a single file to another location:
UNIX
cp file1.txt /path/to/destination/
2. Copy Multiple Files to a Directory
To copy multiple files to a directory, specify the files followed by the destination directory:
UNIX
cp file1.txt file2.txt file3.txt /path/to/destination/
3. Copy a Directory and Its Contents
To copy an entire directory and its contents, use the -r
(recursive) option:
UNIX
cp -r source_directory /path/to/destination/
4. Copy Files Verbosely
To display information about the copying process, use the -v
(verbose) option:
UNIX
cp -v file1.txt /path/to/destination/
5. Prompt Before Overwriting Files
To prompt for confirmation before overwriting existing files, use the -i
(interactive) option:
HTML
cp -i file1.txt /path/to/destination/