Skip to content

/errors/coreutils/cp-r-not-specified-omitting-directory

cp: -r not specified; omitting directory 'dir'

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · ls (GNU coreutils) 8.32updated
tool
GNU coreutils
exit code
1
versions
GNU coreutils 8.x – 9.x
os
linux, windows

GNU cp copies only files unless you ask for recursion. When a source is a directory it prints cp: -r not specified; omitting directory 'dir', skips it, and exits with status 1. Add -r (or -R) to copy the directory and everything in it, or -a to also keep permissions, timestamps and symlinks.

Older coreutils versions print the same error as cp: omitting directory 'dir'. On macOS (BSD cp) the message is cp: dir is a directory (not copied).; the fix is the same.

Reproduce it

bash
mkdir -p project/src && echo 'x' > project/src/main.c && echo 'y' > project/README
mkdir backup
cp project backup/

Output · exit 1

cp: -r not specified; omitting directory 'project'

With a glob, files are copied and directories skipped, and the exit status is still 1:

bash
mkdir -p project/src && echo 'x' > project/src/main.c && echo 'y' > project/README
mkdir backup
cp project/* backup/ || echo "cp exit status: $?"
ls backup

Output

cp: -r not specified; omitting directory 'project/src'
cp exit status: 1
README

Fix

bash
mkdir -p project/src && echo 'x' > project/src/main.c && echo 'y' > project/README
cp -r project copy1
cp -a project copy2
mkdir copy3 && cp -r project/. copy3/
find copy1 copy2 copy3 -type f | sort

Output

copy1/README
copy1/src/main.c
copy2/README
copy2/src/main.c
copy3/README
copy3/src/main.c
CommandResult
cp -r project destdest/project/... if dest exists, otherwise dest becomes the copy
cp -r project/. dest/Contents of project copied into dest, including hidden files
cp -a project destRecursive, keeps mode, ownership, timestamps and symlinks
cp -rn project destRecursive, never overwrite existing files

Sources