The error cp: omitting directory appears when you run cp on a directory without the -r (recursive) option. The fix is to add -r (or -R, -a) to the command, for example cp -r source_dir/ destination/. This article explains why the error occurs, how to copy directories correctly, and how to avoid common mistakes.
Why the Error Occurs
The cp command in Linux is designed to copy files. When you point it at a directory without any flags, it does not know how to handle the directory’s contents. Instead of copying the directory tree, it prints a warning and skips it entirely. The message is intentionally vague — it does not say “use -r” explicitly, but that is exactly what it means.
Directories are not just containers; they hold permissions, timestamps, and nested subdirectories. A simple file copy operation cannot preserve those attributes or recurse into subfolders. The -r flag tells cp to traverse the directory recursively, copying every file and subdirectory inside.
How to Copy a Directory with cp
Add one of these flags to your cp command:
-r— recursive, copies directories and their contents.-R— same as-ron most systems (POSIX specifies-R).-a— archive mode, equivalent to-rplus preservation of permissions, timestamps, and symlinks.
Example commands:
cp -r myfolder/ backup/— copies the foldermyfolderintobackup/.cp -a myfolder/ backup/— same as above but preserves file attributes.cp -R myfolder/ backup/— works on most Linux distributions.
If you want to copy the contents of a directory (not the directory itself), add a trailing slash to the source:
cp -r myfolder/* backup/
This copies all files and subdirectories inside myfolder directly into backup, without creating a backup/myfolder subdirectory.
Choosing Between -r, -R, and -a
All three flags enable recursive copying, but they differ in subtle ways:
-r— the most common and widely supported. It copies directories recursively but does not preserve all file attributes by default.-R— the POSIX standard flag. On Linux, it behaves identically to-rin most cases.-a— archive mode. It is equivalent to-rplus-p(preserve mode, ownership, timestamps) and-d(preserve symlinks). Use it when you want an exact copy for backup or migration.
For everyday copying, -r is sufficient. For backups or when moving files to a new system, -a is safer because it retains metadata.
Common Mistakes and How to Avoid Them
Here are frequent pitfalls that lead to the “omitting directory” error or other copy problems:
- Forgetting the recursive flag — the most obvious cause. Always double-check your command if you see the error.
- Trailing slash confusion —
cp -r source/ destcopies the contents ofsourceintodest, whilecp -r source destcopies the directory itself. This is a common source of unexpected results. - Destination path missing — if the destination directory does not exist,
cpcreates it. But if you specify a trailing slash on the destination, it must exist. - Using wildcards incorrectly —
cp -r *.txt dest/copies all text files, but if a wildcard matches a directory, you still need-r. - Permissions — if you get a “Permission denied” error, you may need
sudoor to change the destination directory’s permissions.
What About cp -r and Symlinks?
By default, cp -r follows symlinks in the source directory and copies the files they point to, not the symlinks themselves. If you want to preserve symlinks as links, use cp -a or cp -rP (where -P means no dereference). For example:
cp -rP myfolder/ backup/
This copies the symlinks as symlinks, which is often what you want when duplicating a directory structure.
Alternatives to cp for Copying Directories
While cp -r is the standard, other tools offer different features:
- rsync — a powerful tool for copying and syncing directories. It can resume interrupted transfers, preserve permissions, and copy over networks. Basic usage:
rsync -av source/ dest/. - tar — often used for archiving, but can copy directories:
tar -C source -cf - . | tar -C dest -xf -. This is useful for preserving hard links and special files. - cpio — similar to tar, less common but still available.
For most users, cp -r is enough. For large or critical data, rsync is recommended because it can verify integrity and resume after interruptions.
Frequently Asked Questions
What does “cp: omitting directory” mean exactly?
It means cp encountered a directory in the source path, but you did not provide the -r flag, so it skipped that directory. The command may still copy files that are not inside directories.
Is there a way to copy directories without using -r?
No, cp requires a recursive flag (-r, -R, or -a) to copy directories. You could use rsync or tar as alternatives, but they also need their own options to recurse.
Why does cp -r sometimes still show the error?
If you see the error even with -r, you may have a typo in the flag (e.g., -R on a system that does not support it, though most do). Also, check that the source path is correct and that you are not using a wildcard that expands to a directory without the flag applied to that specific argument.
What is the difference between cp -r and cp -a?
cp -a is equivalent to cp -r plus preserving attributes (permissions, timestamps, ownership) and symlinks. Use -a for backups or when you want an exact copy; use -r for simple copying where metadata is not critical.
How do I copy a directory and its contents to another location without creating a subdirectory?
Use a trailing slash on the source: cp -r source/ dest/. This copies the contents of source into dest, so if source contains file.txt, you get dest/file.txt, not dest/source/file.txt.
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
- Mac Restart Problem? Fix the Error
- MacBook Sleep Mode: How to Turn It Off
- Oculus Air Link Not Working on Windows? Fixes
- Windows 10 Can’t Detect Hard Drive? Fixes That Work
- How to Archive Emails in Outlook Automatically or Manually
- Restore iPhone from Backup: iCloud & iTunes Steps