Linux – cp Command

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:

Tutorials dojo strip
UNIX

CP Parameters

ParameterDescription
-a, --archiveCopy files and directories recursively, preserving all attributes
--attributes-onlyCopy only the file attributes, not the actual data
--backup[=CONTROL]Create a backup of each existing destination file
-bSimilar to --backup but without accepting an argument
--copy-contentsCopy the contents of special files recursively
-dSame as --no-dereference and --preserve=links
--debugShow detailed information about the copying process
-f, --forceForce the removal of the destination file if it cannot be opened
-i, --interactivePrompt for confirmation before overwriting existing files
-HFollow command-line symbolic links in the source
-l, --linkCreate hard links instead of copying files
-L, --dereferenceAlways follow symbolic links in the source
-n, --no-clobberDo not overwrite existing files (deprecated, use --update instead)
-P, --no-dereferenceNever follow symbolic links in the source
-pPreserve file attributes like mode, ownership, and timestamps
--preserve[=ATTR_LIST]Preserve the specified attributes during the copy
--no-preserve=ATTR_LISTDo not preserve the specified attributes during the copy
--parentsCopy the source file along with its directory structure
-R, -r, --recursiveCopy directories and their contents recursively
--reflink[=WHEN]Control the creation of CoW (copy-on-write) copies
--remove-destinationRemove each existing destination file before copying
--sparse=WHENControl the creation of sparse files
--strip-trailing-slashesRemove trailing slashes from each source argument
-s, --symbolic-linkCreate symbolic links instead of copying files
-S, --suffix=SUFFIXUse the specified suffix for backup files
-t, --target-directory=DIRECTORYCopy all source arguments into the specified directory
-T, --no-target-directoryTreat 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
-uEquivalent to --update=older
-v, --verboseProvide detailed information about the copy operation
--keep-directory-symlinkPreserve existing symbolic links to directories
-x, --one-file-systemStay within the same file system
-ZSet 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
--helpShow help information and exit
--versionDisplay version information and exit

Examples

1. Copy a Single File

To copy a single file to another location:

UNIX

2. Copy Multiple Files to a Directory

To copy multiple files to a directory, specify the files followed by the destination directory:

UNIX

3. Copy a Directory and Its Contents

To copy an entire directory and its contents, use the -r (recursive) option:

UNIX

4. Copy Files Verbosely

To display information about the copying process, use the -v (verbose) option:

UNIX

5. Prompt Before Overwriting Files

To prompt for confirmation before overwriting existing files, use the -i (interactive) option:

HTML

Linux Playground

Tutorials dojo strip
Scroll to Top