-1

The other day I was on a RHEL7.1 system and I wanted to search for a specific word in the /usr/lib/systemd/system directory where all the systemd unit files are located. I used the grep command as usual. See what happened:

# cd /usr/lib/systemd/system
# grep After *
grep: invalid option -- '.'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

Then, I tried on RHEL7.0, RHEL7.2, RHEL7.3 and got the same result. If I use the grep command in any other directory everything works fine. Who can tell me why I got this strange behavior?

1 Answers1

5

The problem is that there is a file in that directory that begins with a dash, namely -.slice.

When you use * all the file names become arguments to the grep, and -.slice gets interpreted as an attempt to pass an option to grep. Since the first character after the dash is a dot, you get that message:

grep: invalid option -- '.'

You'll find that you have the same problem with shell expansion in the directory if you just try

ls *

Once the issue is understood, the answer is easily found. That is, you can add a double dash to indicate to grep that the list of normal options has ended and all following arguments are to be treated as file names:

grep After -- *
Mark Stosberg
  • 3,901
  • 24
  • 28