The cp command in Linux copies files and directories from one location to another. It works immediately once you know the source and destination paths and basic syntax—no special setup is required beyond having access to a terminal and appropriate permissions. This guide walks you through the core usage, common options, pitfalls, and troubleshooting.
What You'll Need
- A Linux system with a terminal (most distributions include it by default)
- Basic understanding of file paths (absolute like
/home/user/file.txtor relative like./file.txt) - Read permission on the source file(s) and write permission on the destination location
Step-by-Step
- Open your terminal application (e.g., GNOME Terminal, Konsole, or an SSH session if remote).
- Identify the source file or directory you want to copy. For example, to copy a file named
report.txtfrom your current directory, ensure it exists by runningls report.txt. - Decide on the destination. This could be another directory (e.g.,
/tmp), a new filename (e.g.,report_backup.txt), or a folder with a subpath (e.g.,~/backups/). - Run the basic syntax:
cp source destination. For instance,cp report.txt /tmp/report.txtcopiesreport.txtto the/tmpdirectory, keeping the same filename. - To copy a directory and all its contents recursively, use the
-r(or-R) option:cp -r myfolder /home/user/backup. This copiesmyfolderand everything inside it. - If you want to preserve file attributes (timestamps, permissions, ownership, symbolic links), add the
-poption:cp -p report.txt /tmp/report.txt. This keeps the original modification time and permissions intact. - To copy multiple files into a directory, list the sources first, then the directory:
cp file1.txt file2.txt ~/docs/. Ensure the last argument is an existing directory. - Use the
-i(interactive) option to avoid accidental overwrites:cp -i report.txt /tmp/report.txt. You’ll be prompted to confirm before overwriting an existing file.
Common Mistakes
Avoid these frequent errors that cause cp to fail or behave unexpectedly:
- forgetting
-rwhen copying directories—cp myfolder /tmpwithout-rreturnscp: -r or -R is required to copy 'myfolder' - omitting a trailing slash on the destination directory when copying into it—
cp file.txt mydirwill create a file namedmydirifmydirdoesn’t exist, instead of copying into an existing directory namedmydir/. Always ensure the destination directory exists, or usemkdir -pfirst. - using
-awithout understanding it’s equivalent to-dR --preserve=all(archives recursively with preservation of metadata and symlinks). It’s powerful but can cause permission issues if copying as a non-root user. - copying into a non-existent directory without creating it first—always verify the destination path exists with
lsormkdir -p. - confusing
cpwithmv—remembercpduplicates;mvmoves and deletes the original.
Tips
These practical pointers improve efficiency and safety:
- Use
-v(verbose) to see what’s being copied:cp -rv project/ /backup/prints each copied file path, helpful for large operations. - Combine options:
cp -rvp source/ dest/copies recursively, verbosely, and preserves all metadata. - To avoid overwriting without prompts, alias
cptocp -iin your shell config (~/.bashrcor~/.zshrc):alias cp='cp -i'. - When copying symlinks, use
-dto preserve them as symlinks instead of copying the target file:cp -dr linkdir/ newdir/. - Use
--parentsto recreate directory hierarchies:cp --parents dir1/file.txt /tmpcreates/tmp/dir1/file.txt, including intermediate directories. - Test risky operations with
-n(no clobber) to skip existing destinations without prompting:cp -rn source/ dest/.
Troubleshooting
If cp fails or behaves oddly, follow these checks:
- Permission denied: Run
ls -l source_fileto verify read permissions. If missing, usechmod +r source_file(if you own it) or run withsudo(but avoidsudo cpunless needed, as it can alter ownership). - Is a directory error: You tried to copy a directory without
-ror-R. Retry with the recursive flag. - Destination not a directory: You listed multiple sources but the last argument isn’t a directory. Ensure the final argument is an existing directory, or use
mkdir -p dest_dirfirst. - Symbolic link mismatch: If symlinks break after copying, use
-dor-ato preserve them. Check withls -lbefore and after. - Timestamp or permission changed unexpectedly: Add
-pto retain original metadata. Without it,cpsets new timestamps and applies default permissions based on yourumask. - Command not found: Rare but possible on minimal systems—ensure coreutils is installed (e.g.,
sudo apt install coreutilson Debian/Ubuntu).
Frequently Asked Questions
Does cp work across filesystems?
Yes. cp reads data from the source filesystem and writes it to the destination filesystem, so copying between, say, an ext4 and an xfs partition works fine. It doesn’t use hard links or reflinks unless explicitly supported by the filesystem and command (e.g., --reflink=auto on some systems).
How do I copy only newer files?
Use -u (update): cp -ru source/ dest/ copies only files that are newer in the source than in the destination, or missing in the destination. This is useful for incremental backups.
Can cp copy hidden files?
Yes—hidden files (starting with .) are copied by default. For example, cp -r mydir/ backup/ includes .git, .env, etc., if they exist in mydir. If you use wildcards like cp * backup/, hidden files are excluded; use cp -r ./* backup/ or enable shopt -s dotglob in bash first.
What’s the difference between -r, -R, and -a?
-r and -R are functionally identical (recursive copy). -a (archive) is a superset: it includes -r plus -d (preserve symlinks), -p (preserve attributes), and other flags to maintain the original structure and metadata exactly. Use -a for exact backups; -r for simple recursive copies.
Why does cp sometimes overwrite silently?
Because cp defaults to non-interactive behavior. Your shell might have aliased it to cp -i (common in Ubuntu), but if not, use cp -i explicitly or add the alias to your shell config to prevent accidental overwrites.
How do I copy only specific file types?
Use wildcards or find with -exec. For example, cp *.log /tmp/logs/ copies all .log files. For deeper trees, find . -name "*.log" -exec cp {} /tmp/logs/ \; copies all .log files recursively into /tmp/logs/.