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: ./* protects shell commands from file names that start with a dash

Shell
~/tips/tip-shell-dot-slash-glob-dash-names

echo ./*

Stop a file called -rf from being read as an option by rm or mv.

A file can be called -rf or --force. When a glob like * expands, that name lands in the command line where the program reads it as an option, not as a file.

See for yourself in an empty test folder:

touch -- -rf a.txt b.txt
echo *
# -rf a.txt b.txt
echo ./*
# ./-rf ./a.txt ./b.txt

With plain *, a command like rm * would receive -rf as its first argument and switch on recursive, forced deletion. With ./* every name starts with ./, and no program mistakes that for an option. I’d write ./* in scripts every time, it costs two characters.

The other fix is --, which tells most programs that options end there: rm -- *. That works for rm, mv, cp and most GNU tools, but not for every program, while ./ works everywhere because the name itself changes.

Be careful with that test folder. Creating a file named -rf in a real directory is asking for trouble, so do it in a scratch folder from mktemp -d and delete the folder afterwards. And the names in the output now start with ./, which matters if you print or compare them.

Further reading: Bash manual: Filename Expansion, ShellCheck SC2035

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