Linux: Find files without string

Finding files with a string is easy, but finding files without a string …

My problem was to find all files named “.ebuild” containing “epatch” but not containing “eutils” …

Solution 1, slow

From http://stackoverflow.com/questions/4989519/unix-command-to-list-files-containing-string-but-not-containing-another-string:
find /usr/portage/ -name "*.ebuild" -exec grep -lR 'epatch' {} \; | xargs grep -L 'eutils'
same, but writing to a file:
find /usr/portage/ -name "*.ebuild" -exec grep -lR 'epatch' {} \; | xargs grep -L 'eutils' > ebuild_wo_epatch.list

It’slow, but it works. The solution below is superior.

Solution 2, way better

Then I found this one: http://superuser.com/a/296538
find /usr/portage/ -name \*.ebuild -print0 \
| xargs -0 grep -Zli 'epatch' \
| xargs -0 grep -Li 'eutils'

This seems easier to understand – but more important: This is faster, way faster then the above!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: