Linux Command: How to 'Find' Only Text Files

Linux command: How to 'find' only text files?

I know this is an old thread, but I stumbled across it and thought I'd share my method which I have found to be a very fast way to use find to find only non-binary files:

find . -type f -exec grep -Iq . {} \; -print

The -I option to grep tells it to immediately ignore binary files and the . option along with the -q will make it immediately match text files so it goes very fast. You can change the -print to a -print0 for piping into an xargs -0 or something if you are concerned about spaces (thanks for the tip, @lucas.werkmeister!)

Also the first dot is only necessary for certain BSD versions of find such as on OS X, but it doesn't hurt anything just having it there all the time if you want to put this in an alias or something.

EDIT: As @ruslan correctly pointed out, the -and can be omitted since it is implied.

grep only text files

You can use the -r(recursive) and -I(ignore binary) options in grep:

$ grep -rI "TEXTSEARCH" .
  • -I Process a binary file as if it did not contain matching data; this is equivalent to the --binary-files=without-match option.
  • -r Read all files under each directory, recursively; this is equivalent to the -d recurse option.

How to print only txt files on linux terminal?

Because you do not have a file extension (.txt) I would try to do it with exclusion.

ls | grep -v tar.gz

If you have multiple types then use extensions.

How to find all files containing specific text (string) on Linux

Do the following:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search

Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:

  • This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
  • For directories it's possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

This works very well for me, to achieve almost the same purpose like yours.

For more options, see man grep.

How to search for a text in specific files in unix

It might be better to use find, since grep's include/exclude can get a bit confusing:

find -type f -name "*.xml" -exec grep -l 'hello' {} +

This looks for files whose name finishes with .xml and performs a grep 'hello' on them. With -l (L) we make the file name to be printed, without the matched line.

Explanation

  • find -type f this finds files in the given directory structure.
  • -name "*.xml" selects those files whose name finishes with .xml.
  • -exec execute a command on every result of the find command.
  • -exec grep -l 'hello' {} + execute grep -l 'hello' on the given file. With {} + we are refering to the matched name (it is like doing grep 'hello' file but refering to the name of the file provided by the find command). Also, grep -l (L) returns the file name, not the match itself.

How to find text files not containing text on Linux?

The command you quote, ironically enough does exactly what you describe.
Test it!

echo "hello" > a
echo "bye" > b
grep -iL BYE a b

Says a only.


I think you may be confusing -L and -l

find . -print | xargs grep -iL "somestring"

is the inverse of

find . -print | xargs grep -il "somestring"

By the way, consider

find . -print0 | xargs -0 grep -iL "somestring"

Or even

grep -IRiL "somestring" .

How to find all files containing specific text (string) on Linux

Do the following:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search

Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:

  • This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
  • For directories it's possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

This works very well for me, to achieve almost the same purpose like yours.

For more options, see man grep.



Related Topics



Leave a reply



Submit