Synopsis
cp [OPTIONS] SOURCE DEST
cp [OPTIONS] SOURCE... DIRECTORY
Description
The cp command copies files and directories from one location to another. It's one of the most fundamental Linux commands for file management. By default, cp copies files but not directories (unless you use the -r option). The command preserves file contents but creates new inodes, meaning the copy is independent of the original.
Common Options
| Option |
Description |
| -r, -R, --recursive |
Copy directories recursively |
| -i, --interactive |
Prompt before overwrite |
| -f, --force |
Force copy, remove existing destination files if needed |
| -v, --verbose |
Explain what is being done |
| -a, --archive |
Archive mode: same as -dR --preserve=all |
| -u, --update |
Copy only when SOURCE is newer than DEST or DEST is missing |
| -n, --no-clobber |
Do not overwrite existing files |
| -p |
Preserve mode, ownership, and timestamps |
| -s, --symbolic-link |
Make symbolic links instead of copying |
| -l, --link |
Hard link files instead of copying |
| -b, --backup |
Make a backup of each existing destination file |
| --preserve=ATTR_LIST |
Preserve specified attributes (mode, ownership, timestamps, links, xattr, context, all) |
Detailed Examples
Example 1: Basic File Copy
Copy a single file to a new name in the same directory.
$ ls -l report.txt
-rw-r--r-- 1 craig staff 1024 Dec 13 10:30 report.txt
$ cp report.txt report_backup.txt
$ ls -l report*.txt
-rw-r--r-- 1 craig staff 1024 Dec 13 10:30 report.txt
-rw-r--r-- 1 craig staff 1024 Dec 13 11:45 report_backup.txt
Note: The copy has a new timestamp but identical content and permissions. The original file remains unchanged.
Example 2: Copy File to Different Directory
Copy a file to another directory while preserving the filename.
$ cp /home/craig/documents/quarterly_report.pdf /home/craig/backups/
$ ls -l /home/craig/backups/quarterly_report.pdf
-rw-r--r-- 1 craig staff 2048576 Dec 13 11:50 /home/craig/backups/quarterly_report.pdf
Tip: When the destination is a directory, cp automatically uses the source filename. You can also specify a different name: cp source.txt /destination/newname.txt
Example 3: Copy Multiple Files
Copy several files to a destination directory in one command.
$ cp file1.txt file2.txt file3.txt /home/craig/archive/
# Or using wildcards:
$ cp *.log /var/log/archive/
$ ls /var/log/archive/
application.log error.log system.log debug.log
Note: When copying multiple files, the last argument MUST be a directory. If it's not, cp will fail with an error.
Example 4: Recursive Directory Copy
Copy an entire directory structure including all subdirectories and files.
$ tree project/
project/
├── src/
│ ├── main.c
│ └── utils.c
├── include/
│ └── header.h
└── README.md
$ cp -r project/ project_backup/
$ tree project_backup/
project_backup/
├── src/
│ ├── main.c
│ └── utils.c
├── include/
│ └── header.h
└── README.md
Warning: Without -r, attempting to copy a directory will fail: cp: omitting directory 'project/'
Example 5: Archive Mode Copy (Preserve Everything)
Copy files while preserving all attributes: permissions, ownership, timestamps, symbolic links, and extended attributes.
$ ls -la original/
total 24
drwxr-xr-x 4 craig staff 128 Dec 13 09:00 .
drwxr-xr-x 8 craig staff 256 Dec 13 09:00 ..
-rw-r--r-- 1 craig staff 1024 Dec 13 08:30 data.txt
lrwxrwxrwx 1 craig staff 8 Dec 13 08:35 link -> data.txt
$ cp -a original/ backup/
$ ls -la backup/
total 24
drwxr-xr-x 4 craig staff 128 Dec 13 09:00 .
drwxr-xr-x 8 craig staff 256 Dec 13 09:05 ..
-rw-r--r-- 1 craig staff 1024 Dec 13 08:30 data.txt
lrwxrwxrwx 1 craig staff 8 Dec 13 08:35 link -> data.txt
Tip: -a is equivalent to -dR --preserve=all. This is the best option for creating exact backups. Notice the timestamps are identical.
Example 6: Interactive Copy (Prevent Accidental Overwrites)
Prompt before overwriting existing files - essential for safety in production environments.
$ ls -l
-rw-r--r-- 1 craig staff 1024 Dec 13 10:00 important.txt
-rw-r--r-- 1 craig staff 2048 Dec 13 11:00 important.txt.new
$ cp -i important.txt.new important.txt
cp: overwrite 'important.txt'? y
$ ls -l important.txt
-rw-r--r-- 1 craig staff 2048 Dec 13 11:30 important.txt
Note: Many systems alias cp to cp -i by default. Check with alias cp. Use \cp to bypass the alias if needed.
Example 7: Update Only Newer Files
Copy only when the source file is newer than the destination or when the destination doesn't exist.
$ ls -l source/
-rw-r--r-- 1 craig staff 1024 Dec 13 12:00 file1.txt
-rw-r--r-- 1 craig staff 2048 Dec 13 12:30 file2.txt
$ ls -l dest/
-rw-r--r-- 1 craig staff 1024 Dec 13 11:00 file1.txt
-rw-r--r-- 1 craig staff 2048 Dec 13 13:00 file2.txt
$ cp -uv source/*.txt dest/
'source/file1.txt' -> 'dest/file1.txt'
# file2.txt was NOT copied because dest version is newer
Tip: -u is perfect for incremental backups and synchronization tasks. Combine with -v to see what's being updated.
Example 8: Create Symbolic Links Instead of Copying
Save disk space by creating symbolic links to files instead of duplicating data.
$ du -sh /usr/share/docs/large_manual.pdf
50M /usr/share/docs/large_manual.pdf
$ cp -s /usr/share/docs/large_manual.pdf ~/manual.pdf
$ ls -l ~/manual.pdf
lrwxrwxrwx 1 craig staff 33 Dec 13 14:00 /home/craig/manual.pdf -> /usr/share/docs/large_manual.pdf
$ du -sh ~/manual.pdf
4.0K /home/craig/manual.pdf
Note: Symbolic links point to the original file. If the original is deleted or moved, the symlink breaks. Use absolute paths for symlinks that will be accessed from different directories.
Example 9: Backup Existing Files Before Overwriting
Automatically create backups of files that would be overwritten.
$ ls -l
-rw-r--r-- 1 craig staff 1024 Dec 13 10:00 config.conf
$ cp -bv new_config.conf config.conf
'new_config.conf' -> 'config.conf' (backup: 'config.conf~')
$ ls -l config.conf*
-rw-r--r-- 1 craig staff 2048 Dec 13 15:00 config.conf
-rw-r--r-- 1 craig staff 1024 Dec 13 10:00 config.conf~
Tip: Control backup naming with --suffix=SUFFIX (default is ~) or -S. Use --backup=numbered for numbered backups like file.txt.~1~, file.txt.~2~, etc.
Example 10: Preserve Specific Attributes
Selectively preserve only certain file attributes during copy.
$ ls -l source.sh
-rwxr-xr-x 1 root admin 512 Dec 13 08:00 source.sh
# Copy preserving mode (permissions) and timestamps only
$ cp --preserve=mode,timestamps source.sh /home/craig/scripts/
$ ls -l /home/craig/scripts/source.sh
-rwxr-xr-x 1 craig staff 512 Dec 13 08:00 /home/craig/scripts/source.sh
# Ownership changed to craig:staff, but permissions and timestamp preserved
Attribute Options:
- mode - file permissions (rwx)
- ownership - user and group (requires root for full preservation)
- timestamps - access and modification times
- links - hard links
- context - SELinux security context
- xattr - extended attributes
- all - all of the above
Common Gotchas and Important Notes
1. Trailing Slashes Matter with Directories
# These are different:
cp -r /source/dir /dest/ # Creates /dest/dir/
cp -r /source/dir/ /dest/ # Copies contents into /dest/
The trailing slash changes behavior - be consistent!
2. File Overwrites Happen Silently (Without -i)
By default, cp will overwrite destination files without warning. Always use -i in scripts or -n to prevent overwrites entirely.
3. Copying vs. Moving
Remember: cp creates a duplicate. The original remains. If you want to move (not copy), use mv instead. Watch your disk space when copying large files!
4. Permissions When Copying Across Users
Non-root users cannot preserve ownership when copying. Use sudo cp if you need to maintain original ownership, or accept that the copy will be owned by the user running cp.
5. Hard Links Have Limits
Using cp -l (hard links) only works within the same filesystem. Hard links cannot cross filesystem boundaries or link to directories.
Performance Considerations
Verbose Mode for Monitoring
When copying many files, add -v to track progress:
$ cp -rv /large/directory/ /backup/
'/large/directory/file1.dat' -> '/backup/file1.dat'
'/large/directory/file2.dat' -> '/backup/file2.dat'
...
For Extremely Large Copies
Consider using rsync instead of cp for:
- Progress indicators
- Resumable transfers
- Network-aware copying
- Better handling of interrupted transfers
Practical Use Cases
Daily Backup Script
#!/bin/bash
# Backup important configs with timestamp
DATE=$(date +%Y%m%d)
cp -a /etc/important.conf /backup/important.conf.$DATE
Safe Production Deployment
# Always backup before deploying
cp -b /var/www/html/index.php /var/www/html/index.php
cp new_index.php /var/www/html/index.php
Creating Templates
# Copy template for new projects
cp -r /home/craig/templates/python_project/ ./my_new_project/
Related Commands
Exit Status
| Code |
Meaning |
| 0 |
Success - all files copied |
| 1 |
Failure - minor problems (e.g., cannot access some files) |
| 2 |
Serious failure (e.g., cannot access destination directory) |
Quick Reference
# Most common usage patterns:
cp file.txt newfile.txt # Basic copy
cp file.txt /path/to/dest/ # Copy to directory
cp -r dir/ newdir/ # Recursive directory copy
cp -a dir/ backup/ # Archive mode (perfect copy)
cp -i file.txt existing.txt # Interactive (ask before overwrite)
cp -u *.txt /backup/ # Update only newer files
cp -v *.log /archive/ # Verbose output
cp -p file.txt copy.txt # Preserve attributes
cp -n file.txt existing.txt # Never overwrite
cp -s /path/to/file link # Create symlink instead