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.txt or relative like ./file.txt)
  • Read permission on the source file(s) and write permission on the destination location

Step-by-Step

  1. Open your terminal application (e.g., GNOME Terminal, Konsole, or an SSH session if remote).
  2. Identify the source file or directory you want to copy. For example, to copy a file named report.txt from your current directory, ensure it exists by running ls report.txt.
  3. 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/).
  4. Run the basic syntax: cp source destination. For instance, cp report.txt /tmp/report.txt copies report.txt to the /tmp directory, keeping the same filename.
  5. To copy a directory and all its contents recursively, use the -r (or -R) option: cp -r myfolder /home/user/backup. This copies myfolder and everything inside it.
  6. If you want to preserve file attributes (timestamps, permissions, ownership, symbolic links), add the -p option: cp -p report.txt /tmp/report.txt. This keeps the original modification time and permissions intact.
  7. 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.
  8. 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 -r when copying directories—cp myfolder /tmp without -r returns cp: -r or -R is required to copy 'myfolder'
  • omitting a trailing slash on the destination directory when copying into it—cp file.txt mydir will create a file named mydir if mydir doesn’t exist, instead of copying into an existing directory named mydir/. Always ensure the destination directory exists, or use mkdir -p first.
  • using -a without 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 ls or mkdir -p.
  • confusing cp with mv—remember cp duplicates; mv moves 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 cp to cp -i in your shell config (~/.bashrc or ~/.zshrc): alias cp='cp -i'.
  • When copying symlinks, use -d to preserve them as symlinks instead of copying the target file: cp -dr linkdir/ newdir/.
  • Use --parents to recreate directory hierarchies: cp --parents dir1/file.txt /tmp creates /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:

  1. Permission denied: Run ls -l source_file to verify read permissions. If missing, use chmod +r source_file (if you own it) or run with sudo (but avoid sudo cp unless needed, as it can alter ownership).
  2. Is a directory error: You tried to copy a directory without -r or -R. Retry with the recursive flag.
  3. 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_dir first.
  4. Symbolic link mismatch: If symlinks break after copying, use -d or -a to preserve them. Check with ls -l before and after.
  5. Timestamp or permission changed unexpectedly: Add -p to retain original metadata. Without it, cp sets new timestamps and applies default permissions based on your umask.
  6. Command not found: Rare but possible on minimal systems—ensure coreutils is installed (e.g., sudo apt install coreutils on 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/.