sodcalculator

10 Tech How-To

How to Copy a Directory in Linux (cp & rsync)

Guide 10-50491 · Updated · Step by step

Copying a directory in Linux is a routine task, but it trips up beginners because the basic cp command behaves differently on folders than on files. This guide shows you the exact commands to duplicate a folder and everything inside it, using both the built-in cp tool and the more advanced rsync utility. You’ll learn the syntax, the important flags, and how to avoid the most common mistakes.

What You’ll Need

  • A Linux system with a terminal (any distribution works).
  • Write permission on the destination directory (where you want the copy to go).
  • Read permission on the source directory (the one you want to copy).
  • Enough free disk space for the copy.

Step-by-Step

  1. Open a terminal. Use your desktop’s terminal emulator, or press Ctrl+Alt+T on most distributions.
  2. Navigate to the parent of the source directory. For example, if your folder is /home/user/projects/website, you don’t need to be inside it, but you need to know its path. You can use relative or absolute paths.
  3. Copy a directory with cp (the basic method). The most common command is:

    cp -r /path/to/source /path/to/destination

    The -r flag means “recursive” – it copies the directory and all its contents (subdirectories and files). If the destination path doesn’t exist, it will be created as a copy of the source. If the destination is an existing directory, the source folder will be placed inside it.
  4. Copy a directory with rsync (the safer, more flexible method). rsync is not always installed by default. Install it with your package manager (e.g., sudo apt install rsync on Debian/Ubuntu, sudo dnf install rsync on Fedora). Then use:

    rsync -av /path/to/source/ /path/to/destination/

    The -a flag (archive) preserves permissions, timestamps, and other metadata. The -v flag (verbose) shows you what’s being copied. Note the trailing slashes: a trailing slash on the source means “copy the contents of this directory”, while no trailing slash means “copy the directory itself”. For the destination, a trailing slash means “place the contents inside this directory”.
  5. Check the copy. After copying, verify the contents with ls -la /path/to/destination or compare the directory trees with diff -r /path/to/source /path/to/destination.
  6. Copy a directory to a remote server (optional). Use rsync with SSH:

    rsync -av /path/to/source/ user@remote-host:/path/to/destination/

    You’ll be prompted for the remote user’s password or SSH key passphrase.

Common Mistakes

  • Forgetting the -r flag with cp. Without it, cp will fail with an error like “omitting directory”.
  • Using the wrong trailing slash with rsync. A source slash changes what gets copied, and a destination slash changes where it lands. Getting these wrong can result in an extra nested folder or a copy of the folder’s contents instead of the folder itself.
  • Copying a directory into itself. If you try to copy a folder into its own subfolder, you’ll get an infinite loop or an error. Always copy to a different location.
  • Not having write permission on the destination. You’ll get a “Permission denied” error. Use sudo only if you know what you’re doing, or change the destination to a writable location.

Tips

  • Use cp -a instead of cp -r when you need to preserve file attributes. The -a flag (archive) is equivalent to -r plus preserving permissions, ownership, timestamps, and symbolic links.
  • Use rsync for large directories or repeated copies. It only copies changed files, which is much faster and efficient.
  • Add --progress to rsync to see a progress bar. This is helpful for large transfers.
  • Use --dry-run with rsync to see what would be copied without actually copying. This is a great safety check.
  • If you want to copy hidden files (those starting with a dot), cp -r includes them by default. rsync also includes them unless you use a filter.

Troubleshooting

  • Error: “cp: omitting directory ‘source'” – You forgot the -r flag. Add it.
  • Error: “rsync: change_dir#3 … failed: No such file or directory (2)” – The destination directory doesn’t exist. rsync will not create it automatically unless you add a trailing slash and the parent exists. Create the destination first with mkdir -p /path/to/destination.
  • Error: “Permission denied” – You don’t have write access to the destination. Check the directory permissions with ls -ld /path/to/destination. Use sudo only if you are sure the destination is safe.
  • Copy seems to hang or is very slow – If you’re copying over a network, it may be a slow link. Use rsync with --progress to see what’s happening. For local copies, check if the disk is full with df -h.
  • After copying, the files are not identical – Run diff -r /path/to/source /path/to/destination to see the differences. If you used cp -r, it may not have preserved symlinks or hard links. Use cp -a or rsync -a to preserve them.

Frequently Asked Questions

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

-a (archive) is a superset of -r. It preserves permissions, ownership, timestamps, and symbolic links. -r only copies the directory tree recursively, but may not preserve all metadata.

How do I copy a directory and its contents to a new location?

Use cp -r /source /new-location. If /new-location does not exist, it will be created as a copy of /source. If it does exist, you’ll get /new-location/source.

How do I copy only the contents of a directory, not the directory itself?

With rsync, put a trailing slash on the source: rsync -av /source/ /dest/. With cp, you can use a wildcard: cp -r /source/* /dest/ (but this misses hidden files).

How do I copy a directory over SSH?

Use rsync -av /local/dir/ user@host:/remote/dir/. This uses SSH by default. You can also use scp -r /local/dir user@host:/remote/, but rsync is more efficient for large or repeated copies.

Does cp copy hidden files?

Yes, cp -r includes hidden files (files starting with a dot) by default. If you use a wildcard like *, hidden files are not included unless you also use .* or the dotglob shell option.

Can I copy a directory without overwriting existing files?

Yes, with rsync use the --ignore-existing flag: rsync -av --ignore-existing /source/ /dest/. This will skip files that already exist in the destination.

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 →