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.
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/
-p
retains permissions, ownership, and timestamps.
Interactive Mode (Prompt Before Overwriting)
cp -i source.txt /target/
-i
asks for confirmation if the destination file exists.
Force Overwrite Without Confirmation
cp -f source.txt /target/
-f
overwrites 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/
-v
prints 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/
-d
copies 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/
-a
combines-r
,-p
,-d
to preserve all attributes and structure.
Copy Only Newer Files
cp -u source.txt /target/
-u
updates 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
-l
creates 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/
-n
prevents overwrites.- Combine with
-v
to simulate and log actions.
Key Notes:
- Overwrite Risks: Use
-i
or-n
to 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.