🐧 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, -R | Copy directories recursively (required when copying folders) |
-i | Prompt interactively before overwriting an existing destination file |
-u | Copy only when source file is newer than destination or destination is missing |
-p | Preserve file attributes: timestamps, ownership, and mode permissions |
-v | Verbose mode: explain what is being done step-by-step |
-a | Archive mode: equivalent to -dR --preserve=all (ideal for backups) |
-n | Do 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.