cp
Copy files and directories
TLDR
Copy a file to another location
Copy a file into another directory, keeping the filename
Recursively copy a directory's contents to another location (if the destination exists, the directory is copied inside it)
Copy a directory recursively, in verbose mode (shows files as they are copied)
Copy multiple files at once to a directory
Copy all files with a specific extension to another location, in interactive mode (prompts user before overwriting)
Follow symbolic links before copying
Use the full path of source files, creating any missing intermediate directories when copying
SYNOPSIS
cp [OPTION]... SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
PARAMETERS
--archive, -a
Equivalent to -dR --preserve=all. Copies recursively, preserving symbolic links, permissions, ownership, timestamps, and context. Ideal for backups.
--force, -f
If an existing destination file cannot be opened, remove it and try again. Overrides -i and -n.
--interactive, -i
Prompt before overwriting an existing destination file.
--link, -l
Make hard links instead of copying files. This means the new 'copy' will point to the same inode as the original.
--recursive, -r, -R
Copy directories and their contents recursively. Essential for copying folders and all their subdirectories and files.
--symbolic-link, -s
Make symbolic links instead of copying files. Note: this command creates a symbolic link to the source, not a copy of the source content.
--update, -u
Copy only when the SOURCE file is newer than the destination file or when the destination file does not exist. Useful for incremental backups.
--verbose, -v
Explain what is being done. Shows the names of files being copied.
--preserve[=ATTR_LIST], -p
Preserve specified attributes (e.g., mode, ownership, timestamps, context, xattr, links, all). -p is equivalent to --preserve=mode,ownership,timestamps.
DESCRIPTION
The cp command, short for copy, is a fundamental Linux utility used to duplicate files and directories. It creates an exact replica of the specified source file or directory at a new location or with a new name. This command is essential for backing up data, creating working copies, or moving files to different locations while retaining the original. cp handles various scenarios, from simple file-to-file copies to copying multiple files into a directory, and even recursively copying entire directory structures, preserving permissions, timestamps, and ownership if desired. It's a versatile tool for managing file systems efficiently.
CAVEATS
- Permissions: cp requires read permission on the source and write permission on the destination directory. Destination files might inherit permissions from the umask or destination directory, unless explicitly preserved.
- Disk Space: Copying large files or directories consumes significant disk space at the destination. Ensure sufficient free space before operation.
- Overwriting: By default, cp can overwrite existing files without warning if you have write permission. Use -i for interactive prompts or -n (no clobber) to prevent accidental overwriting.
- Symbolic Links: By default, cp copies the file pointed to by a symbolic link. Use -P to copy the symbolic link itself (creating a new link at the destination), or -L to always follow symbolic links. -a (archive mode) implies -P for preserving symbolic links.
- Atomicity: cp operations are generally not atomic for multiple files or large directory structures, meaning a power failure or crash during a copy could leave partially copied or corrupted data.
COPYING A FILE TO A DIRECTORY
When the destination is an existing directory, cp places a copy of the source file inside that directory, retaining its original name.
Example: cp myfile.txt /home/user/documents/
COPYING MULTIPLE FILES
You can copy multiple source files to a single destination directory. The last argument must be the target directory.
Example: cp file1.txt file2.txt /home/user/backup/
COPYING DIRECTORIES RECURSIVELY
To copy an entire directory structure, including all subdirectories and files, use the -r or -R option. For a more robust recursive copy that preserves attributes (like permissions, timestamps, and symbolic links), use -a (archive mode).
Example: cp -r mydir /new/location/
HISTORY
The cp command is a standard utility included in virtually all Unix-like operating systems. It is a fundamental component of the GNU Coreutils package, which provides essential tools for file, shell, and text manipulation on GNU/Linux systems. Its core functionality for duplicating files and directories has remained consistent over decades, serving as a basic building block for shell scripting and system administration. Continuous development ensures compatibility with modern file systems and improved handling of advanced features like extended attributes and sparse files.