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?

  • We might need the --remove-long-lines option for grep.
    – Victor
    Commented Feb 23, 2023 at 23:53

5 Answers 5


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 .

(rmam for reset mode auto-margin, smam to restore it; see man 5 terminfo for details).

or use less -S

grep -r pattern . | less -S
  • 3
    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}' Commented May 5, 2015 at 18:24
  • This is nice but is there any way to undo the tput rmam setting? It was sorta helpful if the grep result is within the first 80 characters or so, but now I'm concerned all of my output will be truncated for future commands.
    – h0r53
    Commented Oct 4, 2021 at 21:05
  • cut is perfect solution for my needs. Commented Dec 6, 2021 at 18:32
  • 1
    @h0r53, see edit. Commented Jan 26, 2023 at 14:35

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
# where are the "too-long lines " files
find . -type f -exec grep --color=always -nHPo '.{0,300}to-srch.{0,300}' {} \;

# now repeat the search by excluding those bastards  
find . -type f ! -name '*.main.js' ! -path './build*' -exec \
    grep --color=always -nHio 'to-srch' {} \;

When getting a lot of results from grep,or similar searches, I usually re-direct the output to a text file, using > results.txt. Don't forget to --exclude the text file or you will see errors.

You can then browse through the results at your leisure

  • 1
    That's a good practice, and for other commands outputs also. But it doesn't provide a focused answer into how to exclude files with very long lines at first place, to avoid including anything from them into the search results.
    – thanasisp
    Commented Dec 16, 2020 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .