Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use find in combination with grep to find all the php files in my web server which have a word (any sequence of characters except whitespace) with more than 50 characters.

I have this base:

find . -name '*.php' -exec fgrep -q '.{50}' {} \; -print

But is not working as expected.


share|improve this question

Personally I use egrep only:

egrep -nor '[^ ]{50,}' .

See man egrep to understand what is happening in the previous command.

share|improve this answer
Thanks for this. My concern is that your command will echo the findings. I want to actully echo the file name as I put in my original question... Can you help me to do that? – Hommer Smith Mar 5 '14 at 19:40
@HommerSmith Please test the command: it will print the file name followed by line number and followed by word. If you want only the filenames, you can use: egrep -or '[^ ]{50,}' . | cut -d: -f1 – Radu Rădeanu Mar 5 '14 at 19:48
@HommerSmith Also, if you want only php files, use: egrep -or '[^ ]{50,}' . | cut -d: -f1 | egrep '*.php$' – Radu Rădeanu Mar 5 '14 at 19:53
Yes, but that is inefficient since it will first have to grep all files. The OP's approach using find is better. Also, while looking for non-blank characters is a good idea, [^ ] only looks for non-space characters and will match \t and \r and \n. Finally, I recommend grep -E instead of the deprecated (see man grep) egrep which is being phased out. – terdon Mar 5 '14 at 20:33

It's not working as expected because -q means quiet, it causes grep to not print anything, it's only useful when testing the exist status of grep. What you want is closer to:

find . -name '*.php' -exec grep -lP '\w{50,}' {} \;

The -l means "print the name of the matching file" and -P activates perl compatible regular expressions so we can use the \w which is shorthand for matching word characters (basically [a-zA-Z0-9-_] but the details depend on your locale. Using [^ ] is a good idea but will count \t and \r and even \n, as well as stranger things like ~@#$%^&*() as words and I am assuming that's not what you want.

If you want to match any non-space character, use this instead:

find . -name '*.php' -exec grep -lP '\S{50,}' {} \;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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