Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 13116  / 2 Years ago, mon, december 13, 2021, 12:04:53
sudo find / -name "*" | xargs grep -sn --color=auto "-j"


Above command returns below:



grep: invalid option -- 'j'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
...


How do I search for string -j?


More From » grep

 Answers
4

In your case "-j" is interpreted by grep as an argument/option, not as a search pattern, even if you quoted it. To make it to be the pattern for what you want to search, just use -eoption:



sudo find / -name "*" | xargs grep -sn --color=auto -e "-j"


or even:



sudo find / -name "*" | xargs grep -sn --color=auto -e -j


The -e argument/option means that the next argument is the pattern. This is from man grep:



   -e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify
multiple search patterns, or to protect a pattern beginning with
a hyphen (-). (-e is specified by POSIX.)


Other ways:




  • use --, as @Rinzwind said in his answer, to make grep to hnow that the options ended.


  • use to escape the hyphen (-):



    sudo find / -name "*" | xargs grep -sn --color=auto "-j"


[#24034] Tuesday, December 14, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nquirewha

Total Points: 256
Total Questions: 109
Total Answers: 122

Location: Namibia
Member since Mon, Feb 21, 2022
2 Years ago
nquirewha questions
Wed, Jan 26, 22, 03:38, 2 Years ago
Mon, Nov 1, 21, 13:50, 3 Years ago
Thu, Dec 1, 22, 09:23, 1 Year ago
;