How to Use Grep to Find Files Containing Strings Longer Than X Characters (No Spaces)


2 views

When working with large codebases or log files, developers often need to locate files containing specific patterns - particularly long continuous strings (without spaces) that might indicate hashes, tokens, or encoded data. The standard grep command needs proper parameterization to solve this efficiently.

Here's the basic command structure:

grep -rE '[^ ]{X,}' /path/to/search

Where X is your minimum string length. For example, to find all files containing strings of 20+ characters:

grep -rE '[^ ]{20,}' /projects/
-r : Recursive search
-E : Extended regex support
[^ ] : Matches non-space characters
{X,} : Requires at least X repetitions

To show filenames only (without match context):

grep -rlE '[^ ]{30,}' .

For case-insensitive search:

grep -riE '[^ ]{15,}' /var/log/

Finding potential API keys in a project (64 chars):

grep -rE '[^ ]{64}' ~/projects/api-service/

Locating long hex strings in log files:

grep -rE '[0-9a-fA-F]{32,}' /var/log/app/

For very large directories, add --binary-files=without-match to skip binary files. Combine with | wc -l to count matches instead of displaying them all.


When working with large codebases or log files, developers often need to locate files containing specific patterns of certain lengths. The grep command, combined with regular expressions, provides an efficient solution for this task.

Here's the fundamental grep command to find strings longer than X characters:

grep -rE '[^[:space:]]{X,}' /path/to/search

Replace X with your desired minimum length. The -r flag enables recursive searching, while -E enables extended regular expressions.

Finding long variable names (20+ chars):

grep -rE '[^[:space:]]{20,}' ./src/

Searching for long hexadecimal strings (32+ chars):

grep -rE '[0-9a-fA-F]{32,}' /var/log/

To exclude binary files and focus only on text files:

grep -rI --include='*.txt' --include='*.js' '[^[:space:]]{15,}' .

The -I flag skips binary files, and --include patterns restrict the search to specific file types.

For large directories, you might want to:

find . -type f -name "*.py" -exec grep -lE '[^[:space:]]{30,}' {} +

This combines find with grep for better performance on specific file types.

To count how many files contain such strings:

grep -rlE '[^[:space:]]{25,}' . | wc -l