cp — Copy Files and Directories in Linux
Learn to copy files and directories with cp: recursive copies, preserving permissions, safe overwrite options, and archive mode for backups.
cp copies files and directories. The key rule to remember: you always need -r to copy a directory. Without it, cp refuses to copy directories at all.
Options
cp Options
| Option | Long Form | Description |
|---|---|---|
-r, -R | --recursive | Copy directories recursively (required for dirs) |
-i | --interactive | Prompt before overwriting |
-f | --force | Force overwrite without prompting |
-n | --no-clobber | Never overwrite existing files |
-u | --update | Copy only when source is newer than destination |
-v | --verbose | Show each file as it's copied |
-p | --preserve | Preserve permissions, ownership, timestamps |
-a | --archive | Archive mode = -dR --preserve=all — best for backups |
How Destination Works
How cp behaves depends on whether the destination already exists and whether it's a directory:
- Destination is a directory → file is copied into that directory keeping its name
- Destination doesn't exist → file is copied with the new name
- Destination exists as a file → file is overwritten (unless
-nor-i)
# ─── Copying files ───────────────────────────────────────────
cp file.txt backup.txt # Copy and rename (backup.txt is new name)
cp file.txt /path/to/dir/ # Copy into directory (keeps name: dir/file.txt)
cp file.txt /path/to/dir/newname.txt # Copy into directory with new name
cp file1.txt file2.txt dir/ # Copy multiple files into a directory
# ─── Copying directories ─────────────────────────────────────
cp -r folder/ folder_backup/ # Copy entire directory tree
cp -r dir1 dir2 destination/ # Copy multiple directories
cp -rv folder/ backup/ # Verbose: see each file copied
# ─── Safety ──────────────────────────────────────────────────
cp -i source.txt dest.txt # Ask before overwriting
cp -n source.txt dest.txt # Never overwrite (silently skip)
cp -u source.txt dest.txt # Only copy if source is newer
# ─── Preserving attributes ───────────────────────────────────
cp -p file.txt backup.txt # Preserve: permissions, owner, timestamps
cp -a folder/ backup/ # Archive mode — preserves EVERYTHING
# Use -a for backups, system copiesUse -a for Backups
cp -a (archive mode) is the recommended way to make a backup copy of a directory. It recursively copies everything and preserves file permissions, ownership, timestamps, and symlinks exactly. A plain cp -r might change timestamps or miss symlinks.
Common Patterns
# Backup a config file before editing it
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
# Now you can safely edit nginx.conf
# Copy all .txt files to a directory
cp *.txt /backup/
# Interactive copy (safe for important files)
cp -ri important-folder/ backup-folder/
# Sync: only copy newer files
cp -ru source/ destination/
# Create a hard link instead of copying (saves disk space)
cp -l file.txt hardlink.txt
# Create a symlink instead of copying
cp -s /path/to/original symlink.txtYou run `cp config.yaml /backup/` and `/backup/` is an existing directory. Where does the file end up?
- Create a file with some content:
echo "important data" > data.txt - Make a backup:
cp data.txt data.txt.bak - Create a directory
backup/and copy data.txt into it - Now try:
cp -n data.txt backup/data.txt— what happens (does it overwrite)? - Create a directory
src/with a few files, then copy the whole thing with-aflag