0

I am trying to find all files in my directory, which contain the string "<3". Doing this should be simple:

grep "<3" *

However, running this prints

grep: <3: No such file or directory

and then proceeds to grep all files for something else... (I'm not sure what exactly, but lines show up containing no 3's at all...)

CAUSE: apparently there was a file -f in my directory, and when it's getting passed into grep with the *, grep is treating it as a flag, causing this behaviour.

Trying to delete this file normally also doesn't work, since rm treats it as a flag as well. Thanks to a suggestion from Nick, this file can be removed with

rm ./-f

closed as off-topic by DopeGhoti, Stephen Rauch, Rui F Ribeiro, Stephen Harris, Alex Aug 11 '17 at 19:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – DopeGhoti, Stephen Rauch, Rui F Ribeiro, Stephen Harris, Alex
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Grep shouldn't be used for this. You can use find locate for i in /path/ but grep is generally for searching output and not so much for files. EDIT: I think you are trying to search for content within the files? In which case I believe you would have to cat them first. – Jesse_b Aug 11 '17 at 19:01
  • 1
    There's nothing wrong with using grep to search files' contents. – DopeGhoti Aug 11 '17 at 19:05
  • 2
    @Jesse_b, cating into grep is bad practice; don't suggest this. – DopeGhoti Aug 11 '17 at 19:09
  • 1
    rm ./-f should work to remove the errant file. – NickD Aug 11 '17 at 19:16
  • 1
    When there is a solution, either ask the person to write the answer, or write yourself the answer. Then mark it/accept it as the solution. Please do not edit the answer in the question, please do not add solved to the title. – Rui F Ribeiro Aug 11 '17 at 19:21
5
grep "<3" -- *

With -- you can determine the end of the options and the beginning of the positional arguments for many GNU programs. Thus a file -l does not cause any harm.

An alternative is

grep "<3" ./*
  • I would like to point out that, this can also be used to delete the -f file. – Alex Aug 11 '17 at 19:29
1

Unable to replicate:

$ echo '<3' > foo
$ grep "<3" *
foo:<3

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