grep/file masks

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search


If file is specified using wildcards (e.g. «*.log» or «*»), bash (the default command-line parser) expands this into a listing of all matching files before passing it to grep. This interferes with grep's ability to search multiple files. It can be overridden with quotes or escapes, but grep does not seem to have any wildcard-processing ability of its own (though grep must do some when it's doing recursion; to be investigated) -- so any wildcards passed through without expansion are seen by grep as names of files that don't exist.

Example: «grep -r [text_to_find] *.log» will not (in most contexts, including those where it's most likely to be used e.g. searching inside «/var/log», because it will skip folders that don't also match the filemask -- i.e. it will only search in folders that end in «.log».

Example: If the file-mask finds too many matches in the current folder, it can generate the error «bash: /bin/grep: Argument list too long» -- even if the number of files with matching contents is zero or quite short. (In one case where I ran into this problem, the directory contained 5014 files.)

In short, grep does not have the ability to search recursively using a file-mask, and any mask entered:

  • will prevent grep from recursively searching any folders whose names do not also match (so, typically will only apply to the current/top folder)
  • may prevent grep from being able to work at all if too many files in the top folder have names that match the wildcard pattern

See bash/globbing for more discussion.