Linux search files for contents

Linux command: How to ‘find’ only text files?

which is very unhandy and outputs unneeded texts such as mime type information. Any better solutions? I have lots of images and other binary files in the same folder with a lot of text files that I need to search through.

16 Answers 16

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.

This is better than peoro’s answer because 1. it actually answers the question 2. It does not yield false positives 3. it is way more performant

You can also use find -type f -exec grep -Iq . <> \; -and -print which has the advantage that it keeps the files in find ; you can substitute -print with another -exec that is only run for text files. (If you let grep print the file names, you won’t be able to distinguish file names with newlines in them.)

@NathanS.Watson-Haigh It shouldn’t, because it should be matching text files immediately. Do you have a specific use case you can share?

find . -type f -exec grep -Il . <> + is much faster. Drawback is that it cannot be extended by another -exec as @lucas.werkmeister suggested

grep -rIl «needle text» my_folder

Why is it unhandy? If you need to use it often, and don’t want to type it every time just define a bash function for it:

function findTextInAsciiFiles < # usage: findTextInAsciiFiles DIRECTORY NEEDLE_TEXT find "$1" -type f -exec grep -l "$2" <>\; -exec file <> \; | grep text > 

put it in your .bashrc and then just run:

findTextInAsciiFiles your_folder "needle text" 

EDIT to reflect OP’s edit:

if you want to cut out mime informations you could just add a further stage to the pipeline that filters out mime informations. This should do the trick, by taking only what comes before : : cut -d’:’ -f1 :

function findTextInAsciiFiles < # usage: findTextInAsciiFiles DIRECTORY NEEDLE_TEXT find "$1" -type f -exec grep -l "$2" <>\; -exec file <> \; | grep text | cut -d ':' -f1 > 

I’m not sure if «grep text» is accurate enough to get exactly all text files — I mean, is there any text file types that have no ‘text’ in the string of its mime type description?

Читайте также:  Input chinese in linux

@kavoir.com: yes. From file manual: «Users depend on knowing that all the readable files in a directory have the word ‘text’ printed.»

Wouldn’t it be a bit more clever to search for text files before grepping, instead of grepping and then filtering out text files?

/proc/meminfo , /proc/cpuinfo etc. are text files, but file /proc/meminfo says /proc/meminfo: empty . I wonder if ’empty’ should be tested in addition to ‘text’, but not sure if also other types could report ’empty’.

find . -type f -print0 | xargs -0 file | grep -P text | cut -d: -f1 | xargs grep -Pil "search" 

This is unfortunately not space save. Putting this into bash script makes it a bit easier.

#!/bin/bash #if [ ! "$1" ] ; then echo "Usage: $0 "; exit fi find . -type f -print0 \ | xargs -0 file \ | grep -P text \ | cut -d: -f1 \ | xargs -i% grep -Pil "$1" "%" 

There are a couple of issues in your script: 1. what if a binary file is named text.bin ? 2. What if a filename contains a : ?

Another way of doing this:

# find . |xargs file <> \; |grep "ASCII text" 

If you want empty files too:

# find . |xargs file <> \; |egrep "ASCII text|empty" 
$ grep -rl "needle text" my_folder | tr '\n' '\0' | xargs -r -0 file | grep -e ':[^:]*text[^:]*$' | grep -v -e 'executable' 

If you want the filenames without the file types, just add a final sed filter.

$ grep -rl "needle text" my_folder | tr '\n' '\0' | xargs -r -0 file | grep -e ':[^:]*text[^:]*$' | grep -v -e 'executable' | sed 's|:[^:]*$||' 

You can filter-out unneeded file types by adding more -e ‘type’ options to the last grep command.

If your xargs version supports the -d option, the commands above become simpler:

$ grep -rl "needle text" my_folder | xargs -d '\n' -r file | grep -e ':[^:]*text[^:]*$' | grep -v -e 'executable' | sed 's|:[^:]*$||' 

silly me. Didn’t notice recursive grep. as I understood it’s actually quite fast even though a bit limited in many applications. +1 for you.

1 . make a small script to test if a file is plain text istext:

find . -type f -exec istext <> \; -exec grep -nHi mystring <> \; 

Here’s a simplified version with extended explanation for beginners like me who are trying to learn how to put more than one command in one line.

If you were to write out the problem in steps, it would look like this:

// For every file in this directory // Check the filetype // If it's an ASCII file, then print out the filename 

To achieve this, we can use three UNIX commands: find , file , and grep .

find will check every file in the directory.

file will give us the filetype. In our case, we’re looking for a return of ‘ASCII text’

Читайте также:  Linux добавить центр сертификации

grep will look for the keyword ‘ASCII’ in the output from file

So how can we string these together in a single line? There are multiple ways to do it, but I find that doing it in order of our pseudo-code makes the most sense (especially to a beginner like me).

find ./ -exec file <> «;» | grep ‘ASCII’

Looks complicated, but not bad when we break it down:

find ./ = look through every file in this directory. The find command prints out the filename of any file that matches the ‘expression’, or whatever comes after the path, which in our case is the current directory or ./

The most important thing to understand is that everything after that first bit is going to be evaluated as either True or False. If True, the file name will get printed out. If not, then the command moves on.

-exec = this flag is an option within the find command that allows us to use the result of some other command as the search expression. It’s like calling a function within a function.

file <> = the command being called inside of find . The file command returns a string that tells you the filetype of a file. Regularly, it would look like this: file mytextfile.txt . In our case, we want it to use whatever file is being looked at by the find command, so we put in the curly braces <> to act as an empty variable, or parameter. In other words, we’re just asking for the system to output a string for every file in the directory.

«;» = this is required by find and is the punctuation mark at the end of our -exec command. See the manual for ‘find’ for more explanation if you need it by running man find .

| grep ‘ASCII’ = | is a pipe. Pipe take the output of whatever is on the left and uses it as input to whatever is on the right. It takes the output of the find command (a string that is the filetype of a single file) and tests it to see if it contains the string ‘ASCII’ . If it does, it returns true.

NOW, the expression to the right of find ./ will return true when the grep command returns true. Voila.

Источник

Find Files Containing Specific Text in Linux

Linux, regardless of the distro you use, comes with a number of GUI tools which allow searching for files. Many modern file managers support file searching right in the file list. However, most of them do not allow you to search inside a file’s contents. Here are two methods you can use to search for file contents in Linux.

Probably, there are more methods available. There’s Catfish, a popular search tool with a search index, which can find your files really quickly. It comes with an option to search for file contents, but it does not work reliably for me.

Читайте также:  Asus xonar one linux

Catfish Linux

I would like to share the methods I use myself.
The first method involves the grep utility, which exists in any distro, even in embedded systems built on busybox.

To find files containing specific text in Linux, do the following.

  1. Open your favorite terminal app. XFCE4 terminal is my personal preference.
  2. Navigate (if required) to the folder in which you are going to search files with some specific text.
  3. Type the following command:

Here are the switches:
-i — ignore text case
-R — recursively search files in subdirectories.
-l — show file names instead of file contents portions. ./ — the last parameter is the path to the folder containing files you need to search for your text. In our case, it is the current folder with the file mask. You can change it to the full path of the folder. For example, here is my command

grep -iRl "linux" /home/user/Documents/winaero

Grep Search File Contents

Grep Search Line Number

Note: Other useful switches you might want to use with grep:
-n — show the line number.
-w — match the whole word.

Another method I use is Midnight Commander (mc), the console file manager app. Unlike grep, mc is not included by default in all Linux distros I’ve tried. You may need to install it yourself.

Find files containing specific text with mc

To find files containing some specific text using Midnight Commander, start the app and press the following sequence on the keyboard:
Alt + Shift + ?
This will open the search dialog.

Mc Search File Contents

Fill in the «Content:» section and press the Enter key. It will find all files with the required text.

Mc Search Results

You can place these files in the left or right panel using the Panelize option and copy/move/delete/view/do whatever you want them.

Mc Search Results Panelize

Midnight Commander is a very time-saving tool when it comes to search.

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won’t take a lot from you, but it will help us grow. Thanks for your support!

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube. View all posts by Sergey Tkachenko

Author Sergey Tkachenko Last updated on: Last updated on: October 8, 2019 Categories Linux Tags Linux Find Files, Linux Find Files Containing Text, Linux Grep

6 thoughts on “Find Files Containing Specific Text in Linux”

The code that you provided helped me. There are also another commands which I cannot remember to find text in files but this one is made it quickly. I have bookmarked this post for further usage. Thank you.

Источник

Оцените статью
Adblock
detector