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
cdand checking locations withpwdandls.
Step-by-Step
- Open a terminal. Use your desktop’s terminal emulator or connect via SSH to the machine where the directory resides.
- Navigate to the parent of the source directory. For example, if you want to copy
/home/user/docs, go to/home/userwithcd /home/user. This makes the command shorter and less error-prone. - Check the source directory. Run
ls -lto confirm the directory name and see its contents. Note the permissions (the first column) to understand what will be copied. - Run the
cpcommand with the recursive flag. The basic syntax is:cp -r source_directory destination
For example, to copydocsinto/home/user/backup, run:cp -r docs /home/user/backup
If/home/user/backupexists, this creates/home/user/backup/docs. If it doesn’t exist, it creates/home/user/backupas a copy ofdocs. - Verify the copy. Run
ls -l /home/user/backupto see the new directory. To check that the contents are identical, usediff -r docs /home/user/backup/docs(if the destination isbackup/docs) ordiff -r docs /home/user/backup(if you copied to a new name). No output fromdiffmeans the files match. - Optional: Preserve attributes. To keep timestamps, ownership, and permissions exactly as the original, add the
-pflag:cp -rp docs /home/user/backup. This is important for system files or when you need to retain the original modification times. - Optional: Copy with progress and verbose output. Use
cp -rvto see each file as it’s copied. The-vflag prints the names, which helps confirm that everything is being copied, especially for large directories.
Common Mistakes
- Forgetting the
-rflag. Runningcp docs backupwithout-ron a directory returns an error likecp: -r not specified; omitting directory 'docs'and copies nothing. - Trailing slash confusion.
cp -r docs/ backup(with a trailing slash) copies the contents ofdocsintobackup, whereascp -r docs backupcopies 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,
cpoverwrites 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
sudoonly if you understand the risks, or choose a different destination.
Tips
- Use absolute paths for clarity. For example,
cp -r /home/user/docs /tmp/backupavoids confusion about the current directory. - Copy hidden files. The
cpcommand includes hidden files (those starting with a dot) by default when copying a directory. If you use a wildcard likecp -r docs/* backup, hidden files are excluded unless you usedotglobin 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.
cpworks across different filesystems (e.g., from ext4 to NTFS) but may not preserve all attributes. For a true clone, considerrsync -a. - Use
rsyncfor 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. Uselsto 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 usesudo(but be careful with ownership). - Error: “cp: omitting directory ‘source'” — You forgot the
-rflag. 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 withdf -hand try again. For large transfers, usersyncwith checksum verification. - Symbolic links are broken after copy. By default,
cp -rfollows symlinks in the source and copies the target files. If you want to copy the symlinks themselves, usecp -aorcp -P(no dereference). - Copy is very slow. If the directory is huge, consider using
rsyncwith the--progressflag 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
- Google Desktop Search Widget on Windows 11
- How to Build a Computer Stand (Saturday Workshop)
- Customize Touch Keyboard Themes in Windows 11
- Excel Pivot Table: Change Multiple Field Settings
- WhatsApp: How to Block Unknown Numbers
- How to Exit Vi in Linux: A Simple Guide