I did: grep -r -H -L txt ~

And grep -r -l -L txt ~

Does it lists all the files? Because I am getting a really long output.

  • it always searches within FILES. Listing directories does not relate to grep at all Nov 23 '17 at 11:50
  • I am sorry, I meant listing the files (-H enables grep to list the files as well). I will update the question. Nov 23 '17 at 12:01
  • -L --files-without-match - Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning of each file stops on the first match. Nov 23 '17 at 12:14
  • Okay. Probably my combination is very useless and that is why it is giving unexpected output. Nov 23 '17 at 12:20
  • @Fox No no. I am just trying to use -l and -L together. I am getting vague output. So that's what happens probably when we use incompatible options together. Nov 23 '17 at 12:33

Your two commands are exactly the same...they both recurse through your home directory and list files that don't contain the pattern 'txt'.

To prove they have the same output you can do

diff <(grep -r -H -L txt ~) <(grep -r -l -L txt ~)

Breaking it down a bit...

grep -r -H -L

is the same as

grep -r -L

-H (print filenames) is implied for any grep call that involves more than one file. That's what -L does since it lists all files that don't match the specified pattern.


grep -r -l -L

is the same as

grep -r -L

This is simply due to the fact that -l and -L are conflicting options and in such cases the last flag to appear overrides any previous conflicting flag(s). (Thanks to @don_crissti for pointing this out.)

So you in effect have grep -r -L and grep -r -L.

  • Regarding your statement "grep -r -l -L is the same as grep -r -L" ... That is true, but not because "-l is implied for any grep etc...", it is true because of the "last option" rule, that is, if you switch -l and -L then the statement becomes "grep -r -L -l is the same as grep -r -l". IOW, when conflicting options are used, the last one cancels out the others. Nov 30 '17 at 11:11
  • @don_crissti Oh, indeed. I convinced myself it was redundant through my testing but must have fat-fingered something along the way. I will update my answer. Thanks for the correction.
    – B Layer
    Nov 30 '17 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.