Skip to content
shellhacks.dev
Command Line

Copy a directory including hidden files

Copy a directory with its dotfiles using cp -a src/. dst/. The usual src/* glob skips hidden files, and the dot form needs no shell option.

1 min read
Summarize in AI

A copy of a directory with cp -r src/* dst/ quietly leaves out every file whose name starts with a dot.

Shell
mkdir -p src dst1 dst2
touch src/app.conf src/.env
cp -r src/* dst1/
cp -a src/. dst2/
ls -A dst1
echo ---
ls -A dst2
Text
app.conf
---
.env
app.conf

The trailing /. tells cp to copy the contents of the directory, including entries that begin with a dot, because the argument names the directory itself rather than expanding to a list of names. The -a option is archive mode in GNU cp: it recurses and keeps links, permissions, ownership and timestamps where it can.

Ownership is only preserved when running as root. The src/. form works in GNU and BSD cp, but -a is GNU and recent BSD; on older systems use cp -pR src/. dst/. The destination must already exist, or cp creates it from the source name rather than copying the contents into it.

Share

More snippets

Command Line

List hidden files only

List hidden files only with ls -d .[!.]* in GNU coreutils 9 or any POSIX shell. The glob skips the dot and double dot entries that .* would match.

1 min read
Command Line

Show the full timestamp of files

Show the full timestamp of every file with ls -l --full-time in GNU coreutils. It prints nanosecond precision and the zone, which ls -l hides.

1 min read
Command Line

Count the files in a directory

Count the files in a directory with ls -A | wc -l, or with find when names may contain newlines. A plain ls -1 skips every hidden file.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.