Prefixing ./
(or any path) prevents filenames starting with a dash from being taken as options.
$ touch ./-l foo bar
$ ls
bar foo -l
$ ls *
-rw-r--r-- 1 ilkkachu ilkkachu 0 Nov 3 15:10 bar
-rw-r--r-- 1 ilkkachu ilkkachu 0 Nov 3 15:10 foo
The root issue is that it's the shell that expands the wildcard, and when e.g. ls
sees the argument -l
, it can't know if it came from a filename wildcard, or if the user wrote it by hand.
Above, the file called -l
caused ls
to switch to the long listing. Another such filename could have ended up with an invalid option
error. Or worse, if the command was rm
, and you had a file called -rf
.
That's just one of the issues that's arguably "wrong" with the relaxed attitude Unix systems take on file names. For more than you want to know, see e.g. these essays from David Wheeler:
touch -a
vstouch ./-a
. – Quasímodo Nov 03 '20 at 13:07