0

As I understand it:

  • ./filename executes filename as a script.
  • ./ indicates the current directory.

What purpose (if any) does the ./ serve if precedes a glob qualifier (filter):

qpdf --empty --pages ./*.pdf(nOn) -- output.pdf

The question is posed because execution without the ./ seems to produce the same result.

gatorback
  • 1,216
  • 20
  • 44
  • 1
    Probably related to this: [grep getting confused by filenames with dashes](https://unix.stackexchange.com/questions/364922/grep-getting-confused-by-filenames-with-dashes) – steeldriver Nov 03 '20 at 13:06
  • 1
    That is usually a protection against files starting with a dash being interpreted as options to the command. Consider `touch -a` vs `touch ./-a`. – Quasímodo Nov 03 '20 at 13:07
  • 1
    relating https://unix.stackexchange.com/a/302059/117549 and https://unix.stackexchange.com/q/110750/117549 and https://unix.stackexchange.com/a/170014/117549 and https://unix.stackexchange.com/a/490535/117549 – Jeff Schaller Nov 03 '20 at 13:08
  • 1
    See also [What is the difference between "du -sh \*" and "du -sh ./\*"?](//unix.stackexchange.com/q/110750) – Stéphane Chazelas Nov 03 '20 at 13:15

2 Answers2

2

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:

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
1

The dot-slash construct doesn't "execute filename as a script" - it is a directive to the shell (or whatever other program is interpreting the input) to "look in the current directory". It is most often prepended to a command (binary or script) when that command is in the current working directory and when the current working directory is not in the search path. Using it within a command (as in your qpdf example) is a way to get source files from a different directory when you expect output files to drop into the directory you're in when you invoke the command.

John
  • 16,759
  • 1
  • 34
  • 43