🐧 Linux cp Command

Copy files and directories across folders, filesystems, and storage volumes.

Syntax

$ cp [options] source_path destination_path

Common Command Options

Option Description
-r, -RCopy directories recursively (required when copying folders)
-iPrompt interactively before overwriting an existing destination file
-uCopy only when source file is newer than destination or destination is missing
-pPreserve file attributes: timestamps, ownership, and mode permissions
-vVerbose mode: explain what is being done step-by-step
-aArchive mode: equivalent to -dR --preserve=all (ideal for backups)
-nDo not overwrite an existing file (no-clobber)

Practical Examples

Copy a Single File
cp document.pdf document_backup.pdf
Creates a duplicate of document.pdf in the current directory.
Copy File into a Folder
cp notes.txt /home/ubuntu/docs/
Copies notes.txt into the /home/ubuntu/docs/ folder.
Copy Directory Recursively
cp -r /var/www/site /var/www/site_backup
Copies the entire site directory and all its files and subdirectories.
Interactive Overwrite Protection
cp -i config.json /etc/app/config.json
Asks you for confirmation (y/n) before overwriting an existing config file.
Update Backup Directory
cp -u *.jpg /backups/photos/
Only copies JPG files that are newer than the copies in the destination folder.