Skip to content
shellhacks.dev
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
Summarize in AI

You want the dotfiles in a directory and nothing else, without the two special entries every directory has.

Shell
mkdir demo && cd demo
touch .env .gitignore ..odd visible.txt
ls -d .[!.]*
echo ---
ls -d .*
Text
.env
.gitignore
---
..odd
.env
.gitignore

The glob .[!.]* means a dot, one character that is not a dot, then anything, so it skips . and .. in every shell. The -d option from GNU ls stops it listing the contents of any matching directory. The plain .* pattern is shorter, and Bash 5.2 turns on globskipdots so it drops the two special entries too, as the second list shows.

Before Bash 5.2, and in dash and zsh, .* also matches . and .., so the plain pattern is not portable. A name that starts with two dots, like ..odd, is missed by .[!.]*. Add ..?* as a second pattern if you need those. ls -A lists everything except the two special entries.

Share

More snippets

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
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

Get new posts by email

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

Unsubscribe in one click. No spam, ever.