Copying files and folders in the Linux command line using the cp command is a routine administrative task. However, attempting to copy a folder without providing specific flags causes the terminal to reject the operation with a standard message: cp: -r not specified; omitting directory 'dirname' (or simply cp: omitting directory 'dirname'). The command completes with an exit status code, but the targeted directory and all files inside remain untouched in their original location.
Why Does cp Omit Directories by Default?
In Unix and Linux architecture, directories are special filesystem nodes that contain directory table lists rather than raw byte content. The cp command enforces specific safety standards:
- Default Shallow File Handling: By default, the POSIX specification for
cpdictates that the utility must operate exclusively on regular files, character devices, or symbolic links unless instructed to traverse filesystem hierarchies recursively. - Accidental Deep Copy Prevention: Preventing users from inadvertently duplicating nested multi-gigabyte directory trees, endless recursive symlinks, or special device files located in system trees.
- Missing Recursion Flags: Omitting the recursive flags (
-r,-R, or-a) when passing directory names as arguments.
Step 1: Use the Recursive Flag (-r or -R)
Instructing cp to traverse directories recursively copies the parent folder along with all nested subdirectories and files.
- To copy a directory and its entire contents to another destination, pass the
-r(or-R) flag:cp -r /path/to/source_directory /path/to/destination/ - For example, to copy a project folder to your backups directory:
cp -r my_project/ ~/backups/ - The directory and all internal files will be created in the target destination without error.
Step 2: Preserve File Attributes Using Archive Mode (-a)
When copying configuration files, scripts, or user data, use archive mode to preserve permissions, timestamps, and symbolic links.
- Pass the
-aflag (equivalent to-dR --preserve=all):cp -a source_directory/ /destination/ - Archive mode ensures that file creation dates, modification timestamps, user and group ownerships, and symbolic links are preserved exactly as they exist in the source directory.
- This is the recommended standard when backing up server configuration directories or website document roots.
Step 3: Copy Directory Contents Without Creating a Nested Folder
If you want to copy the contents of a directory into an existing folder rather than nesting the parent folder inside, use trailing dot syntax.
- To copy all files and subdirectories inside
folderAintofolderB:cp -r folderA/. folderB/ - The trailing
/.syntax instructs cp to copy everything inside the directory (including hidden dotfiles such as.envor.gitignore) without creating an extrafolderB/folderA/subfolder.
Step 4: Use rsync for Advanced Directory Copying and Progress Tracking
For large directories, rsync provides superior resilience, progress reporting, and resumable transfers.
- Run
rsyncwith archive and human-readable progress flags:rsync -avP source_directory/ destination_directory/ - The
-aflag preserves all attributes recursively, while-vPdisplays a live transfer progress bar for each file. - Unlike
cp, if a network transfer or disk copy is interrupted, running the identical rsync command resumes from the exact byte where it paused, avoiding redundant data copying.