brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: shopt -s dotglob makes * match hidden files too

Shell
~/tips/tip-shell-dotglob-hidden-files

shopt -s dotglob

Lets the * wildcard include files whose names start with a dot.

Anyone who ran mv * /somewhere and left .env behind knows that * skips dotfiles on purpose.

Bash has a switch for it. The manual says that with dotglob set, filename expansion includes names that begin with a dot:

shopt -s dotglob
mv /mnt/user/appdata/<app>/* /mnt/user/appdata/<app>-old/
shopt -u dotglob

The last line switches it back off. In an interactive shell I would do that so later commands behave the way you expect. In a script it is cleaner to set it once at the top.

You can see the effect with echo, which is harmless:

cd "$(mktemp -d)" && touch .hidden visible
echo *          # visible
shopt -s dotglob
echo *          # .hidden visible

The names . and .. are not matched by the star. The manual says they must always be matched explicitly even with dotglob set, and in newer bash the globskipdots option, on by default, also keeps them out of patterns that start with a dot.

Because it changes what every glob matches, rm -r * now removes hidden files as well, such as a .git folder or an .env. Run echo * first before you point a destructive command at a folder.

When you need hidden files only once, the pattern .[!.]* matches them without changing any option, though it skips names that start with two dots.

Further reading: bash(1) manual page

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link