Sunday, May 5, 2024
22
rated 0 times [  22] [ 0]  / answers: 1 / hits: 1173  / 3 Years ago, wed, july 7, 2021, 10:12:53

This may be a simple question, but how do I use files starting with "-" with command line programs?



For example, I am trying to use pdfgrep on a file named -2013-01-01.pdf but it complains that there are no options defined for 2, 0, 1, 3 and so on.



Changing the filename to something that does not start with "-" solves it, but this is not an option since the files aren't created by me. I simply want to check for a specific change I know is coming.


More From » command-line

 Answers
6

Very frequently -- is used on the command-line to signal to a program that no more available command switches are going to be used. This is particularly useful if a file contains a dash, which the program would try to interpret as an option.




  1. Without the --, there is an error generated:



    $ pdfgrep -i posix -find.pdf -xorg.pdf

    pdfgrep: invalid option -- 'f'
    pdfgrep: invalid option -- 'd'
    pdfgrep: invalid option -- '.'
    pdfgrep: invalid option -- 'p'
    pdfgrep: invalid option -- 'd'
    pdfgrep: invalid option -- 'f'

  2. With the -- used we have a successful command:



    $ pdfgrep -i posix -- -find.pdf -xorg.pdf

    -find.pdf: on the command line. Currently-implemented types are emacs (this is the default), posix-awk,
    -find.pdf: posix-basic, posix-egrep and posix-extended.
    -find.pdf: posix-basic, posix-egrep and posix-extended.
    -find.pdf: posix-basic, posix-egrep and posix-extended.

  3. pdfgrep is programmed to understand -- to mean that the following command-line arguments are not options. Most programs do the same, but not all programs understand --. For programs that don't, the solution is to prepend the filename with ./, like this:



     pdfgrep -i posix ./-find.pdf ./-xorg.pdf


    This should work with any command, unless for some reason the command cannot accept a path, only a pure filename.




For a general introduction to the command-line, see this useful PDF.


[#31887] Friday, July 9, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
piscen

Total Points: 134
Total Questions: 117
Total Answers: 133

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
;