sodcalculator

10 Tech How-To

Copy a Directory in Linux: Full Guide

Guide 10-12196 · Updated · Step by step

Copying a directory in Linux duplicates the folder and everything inside it—files, subfolders, and often permissions—to a new location. This guide covers the cp command with the -r (recursive) flag, which is the standard way to copy directories, plus practical examples, common pitfalls, and troubleshooting tips.

What You’ll Need

  • A Linux terminal (shell) with a user account that has read permission on the source directory and write permission on the destination location.
  • Basic familiarity with navigating the filesystem using cd and checking locations with pwd and ls.

Step-by-Step

  1. Open a terminal. Use your desktop’s terminal emulator or connect via SSH to the machine where the directory resides.
  2. Navigate to the parent of the source directory. For example, if you want to copy /home/user/docs, go to /home/user with cd /home/user. This makes the command shorter and less error-prone.
  3. Check the source directory. Run ls -l to confirm the directory name and see its contents. Note the permissions (the first column) to understand what will be copied.
  4. Run the cp command with the recursive flag. The basic syntax is:
    cp -r source_directory destination
    For example, to copy docs into /home/user/backup, run:
    cp -r docs /home/user/backup
    If /home/user/backup exists, this creates /home/user/backup/docs. If it doesn’t exist, it creates /home/user/backup as a copy of docs.
  5. Verify the copy. Run ls -l /home/user/backup to see the new directory. To check that the contents are identical, use diff -r docs /home/user/backup/docs (if the destination is backup/docs) or diff -r docs /home/user/backup (if you copied to a new name). No output from diff means the files match.
  6. Optional: Preserve attributes. To keep timestamps, ownership, and permissions exactly as the original, add the -p flag: cp -rp docs /home/user/backup. This is important for system files or when you need to retain the original modification times.
  7. Optional: Copy with progress and verbose output. Use cp -rv to see each file as it’s copied. The -v flag prints the names, which helps confirm that everything is being copied, especially for large directories.

Common Mistakes

  • Forgetting the -r flag. Running cp docs backup without -r on a directory returns an error like cp: -r not specified; omitting directory 'docs' and copies nothing.
  • Trailing slash confusion. cp -r docs/ backup (with a trailing slash) copies the contents of docs into backup, whereas cp -r docs backup copies the directory itself. This subtle difference can lead to unexpected nesting.
  • Destination inside the source. Copying a directory into itself (e.g., cp -r docs docs/sub) can cause infinite recursion and errors. Always ensure the destination is outside the source.
  • Overwriting without warning. By default, cp overwrites existing files silently. If you want to be prompted, use the -i (interactive) flag.
  • Not checking permissions. If you get a permission denied error, the destination directory might not be writable by your user. Use sudo only if you understand the risks, or choose a different destination.

Tips

  • Use absolute paths for clarity. For example, cp -r /home/user/docs /tmp/backup avoids confusion about the current directory.
  • Copy hidden files. The cp command includes hidden files (those starting with a dot) by default when copying a directory. If you use a wildcard like cp -r docs/* backup, hidden files are excluded unless you use dotglob in bash.
  • Preserve hard links. Use cp -a (archive) which is equivalent to -dR --preserve=all. This preserves hard links, symbolic links, and all attributes—ideal for making a backup.
  • Copy across filesystems. cp works across different filesystems (e.g., from ext4 to NTFS) but may not preserve all attributes. For a true clone, consider rsync -a.
  • Use rsync for large or repeated copies. rsync -av source/ destination/ is more efficient for incremental backups and can resume interrupted transfers.

Troubleshooting

  • Error: “cp: cannot stat ‘source’: No such file or directory” — The source path is wrong. Check the spelling and the current directory with pwd. Use ls to list the parent.
  • Error: “cp: cannot create regular file ‘destination’: Permission denied” — You don’t have write permission on the destination. Check with ls -ld destination. Change the destination or use sudo (but be careful with ownership).
  • Error: “cp: omitting directory ‘source'” — You forgot the -r flag. Add it and retry.
  • Copy is incomplete or files are corrupted. Verify with diff -r source destination. If there are differences, check for disk space with df -h and try again. For large transfers, use rsync with checksum verification.
  • Symbolic links are broken after copy. By default, cp -r follows symlinks in the source and copies the target files. If you want to copy the symlinks themselves, use cp -a or cp -P (no dereference).
  • Copy is very slow. If the directory is huge, consider using rsync with the --progress flag to monitor. Also, check if the source and destination are on the same disk; copying across disks is slower.

Frequently Asked Questions

How do I copy a directory and its contents to another directory?

Use cp -r /path/to/source /path/to/destination. If the destination exists, the source folder is placed inside it. If it doesn’t, the destination becomes a copy of the source.

What is the difference between cp -r and cp -a?

cp -r copies directories recursively but may not preserve all attributes. cp -a (archive) is a combination of -dR --preserve=all, which preserves symlinks, permissions, timestamps, and ownership where possible. Use -a for backups.

How do I copy a directory without following symbolic links?

Use cp -P (or cp -a which includes -d). This copies the symlinks themselves, not the files they point to.

Can I copy a directory and exclude certain files?

Yes, but cp doesn’t have an exclude option. Use rsync instead: rsync -av --exclude='*.tmp' source/ destination/.

What does the -i flag do?

The -i (interactive) flag prompts you before overwriting any existing files. It’s a safety net to avoid accidental data loss.

How do I copy a directory over SSH to a remote server?

Use scp -r /local/dir user@remote:/path/to/dest or rsync -av /local/dir user@remote:/path/to/dest. The latter is more efficient for large directories.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →