I often run grep commands to find things in my code, but the problem with web projects is that there will often be compressed JavaScript and CSS files which create one huge line of text, so that if a match is found, the whole terminal window is filled for more then a 1000 lines, making it extremely impractical to find what I'm looking for.

So is there a way to avoid files that have say single lines of text over 200 characters?


With GNU grep and xargs:

grep -rLZE '.{200}' . | xargs -r0 grep pattern

Alternatively, you could cut the output of grep:

grep -r pattern . | cut -c1-"$COLUMNS"

or tell your terminal not to wrap text if it supports it:

tput rmam
grep -r pattern .

or use less -S

grep -r pattern . | less -S
  • 2
    Using regex from your first example, piping into grep with invert match, ... | grep -v -E '.{200}', works, too. E.g. to find all lines of *.js files under current dir with ".name" in them that are no longer than 200 chars: find . -name "*.js" -exec grep -H \\.name {} \; | grep -v -E '.{200}' – Gary S. Weaver May 5 '15 at 18:24

Option 1: You can exclude files matching a certain pattern:

grep --exclude='*.min.*'

This will exclude script.min.js and style.min.css... Other grep option include --exclude-from=FILE and --exclude-dir=DIR

Option 2: I am not sure if this is practical, but you can cut the first 200 chars of each line, and then grep them:

grep -H [OPTIONS] PATTERN [FILE...] | cut -c1-200 | grep PATTERN

The first grep does an initial match and output the file name and the line, the second one ensures the PATTERN is still there after cutting the lines.


In this kind of situation, I like to grep a pattern with a neighborhood context (lets say 30 chars):

grep -Po '.{0,30}pattern.{0,30}' *.js

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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