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
- Open a terminal. Use your desktop’s terminal emulator, or press
Ctrl+Alt+Ton most distributions. - 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. - Copy a directory with
cp(the basic method). The most common command is:
cp -r /path/to/source /path/to/destination
The-rflag 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. - Copy a directory with
rsync(the safer, more flexible method).rsyncis not always installed by default. Install it with your package manager (e.g.,sudo apt install rsyncon Debian/Ubuntu,sudo dnf install rsyncon Fedora). Then use:
rsync -av /path/to/source/ /path/to/destination/
The-aflag (archive) preserves permissions, timestamps, and other metadata. The-vflag (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”. - Check the copy. After copying, verify the contents with
ls -la /path/to/destinationor compare the directory trees withdiff -r /path/to/source /path/to/destination. - Copy a directory to a remote server (optional). Use
rsyncwith 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
-rflag withcp. Without it,cpwill 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
sudoonly if you know what you’re doing, or change the destination to a writable location.
Tips
- Use
cp -ainstead ofcp -rwhen you need to preserve file attributes. The-aflag (archive) is equivalent to-rplus preserving permissions, ownership, timestamps, and symbolic links. - Use
rsyncfor large directories or repeated copies. It only copies changed files, which is much faster and efficient. - Add
--progresstorsyncto see a progress bar. This is helpful for large transfers. - Use
--dry-runwithrsyncto 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 -rincludes them by default.rsyncalso includes them unless you use a filter.
Troubleshooting
- Error: “cp: omitting directory ‘source'” – You forgot the
-rflag. Add it. - Error: “rsync: change_dir#3 … failed: No such file or directory (2)” – The destination directory doesn’t exist.
rsyncwill not create it automatically unless you add a trailing slash and the parent exists. Create the destination first withmkdir -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. Usesudoonly 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
rsyncwith--progressto see what’s happening. For local copies, check if the disk is full withdf -h. - After copying, the files are not identical – Run
diff -r /path/to/source /path/to/destinationto see the differences. If you usedcp -r, it may not have preserved symlinks or hard links. Usecp -aorrsync -ato 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
- Windows 11 Calendar Wrong Day? Fix It Here
- How to Use curl Command in Linux
- Install ChromeOS: Convert Laptop to Chromebook
- Fix OneNote App Not Working on Android
- Move Games to SSD Without Reinstalling
- Move Windows 11 Taskbar to Top or Side: Easy Steps