The cp command in Linux stands for “copy”. It is used to duplicate files and directories from one location to another within the filesystem. The command preserves the original files and creates identical copies at the specified destination. Key features include copying single/multiple files, recursive directory copying, preserving file attributes (permissions, timestamps), and handling overwrites safely.
- Copy a File to a Directory
- Copy and Rename a File
- Copy Multiple Files to a Directory
- Copy a Directory Recursively
- Preserve File Attributes
- Interactive Mode (Prompt Before Overwriting)
- Force Overwrite Without Confirmation
- Backup Existing Files
- Copy All Files in a Directory
- Verbose Mode (Show Progress)
- Copy Hidden Files
- Preserve Symbolic Links
- Copy Files with Wildcards
- Archive Mode (Preserve Everything)
- Copy Only Newer Files
- Exclude Specific Files
- Copy to a Remote Server via SSH
- Create Hard Links Instead of Copies
- Copy Sparse Files Efficiently
- Dry Run (Simulate Copying)
Use cp to back up data, organize files, or distribute content across directories. Always exercise caution, as it can overwrite existing files by default.
Copy a File to a Directory
cp file.txt /target/directory/
Copies file.txt to /target/directory, retaining the original name.
Copy and Rename a File
cp file.txt newfile.txt
Copies file.txt to newfile.txt in the same directory (renames during copy).
Copy Multiple Files to a Directory
cp file1.txt file2.txt /backup/
Copies both files to the /backup directory.
Copy a Directory Recursively
cp -r my_directory/ /backup/
-r(or-R) copies directories and their contents recursively.
Preserve File Attributes
cp -p source.txt /backup/
-pretains permissions, ownership, and timestamps.
Interactive Mode (Prompt Before Overwriting)
cp -i source.txt /target/
-iasks for confirmation if the destination file exists.
Force Overwrite Without Confirmation
cp -f source.txt /target/
-foverwrites existing files silently (use with caution).
Backup Existing Files
cp --backup=numbered source.txt /target/
Creates a numbered backup (e.g., source.txt.~1~) if the destination exists.
Copy All Files in a Directory
cp -r /source/* /destination/
Copies all files/subdirectories from /source to /destination.
Verbose Mode (Show Progress)
cp -v file.txt /target/
-vprints details:'file.txt' -> '/target/file.txt'.
Copy Hidden Files
cp -r .hidden_dir/ /backup/
Copies hidden files/directories (names starting with .).
Preserve Symbolic Links
cp -d symlink /backup/
-dcopies symlinks as links instead of their target files.
Copy Files with Wildcards
cp *.jpg /images/
Copies all .jpg files to the /images directory.
Archive Mode (Preserve Everything)
cp -a /source/ /backup/
-acombines-r,-p,-dto preserve all attributes and structure.
Copy Only Newer Files
cp -u source.txt /target/
-uupdates the destination only if the source is newer.
Exclude Specific Files
cp -r --exclude="*.tmp" /source/ /destination/
Copies all files except those ending with .tmp.
Copy to a Remote Server via SSH
scp -r local_dir/ user@remote:/path/
Uses scp (Secure Copy Protocol) for remote transfers (technically not cp but related).
Create Hard Links Instead of Copies
cp -l source.txt link.txt
-lcreates hard links (saves disk space; changes reflect in both files).
Copy Sparse Files Efficiently
cp --sparse=always large_file.img /backup/
Optimizes space usage for sparse files (e.g., virtual disk images).
Dry Run (Simulate Copying)
cp -nvr /source/ /destination/
-nprevents overwrites.- Combine with
-vto simulate and log actions.
Key Notes:
- Overwrite Risks: Use
-ior-nto avoid accidental data loss. - Permissions: Ensure you have read access to the source and write access to the destination.
- Wildcards: Use
*(any characters) or?(single character) for pattern matching.
