Find file in linux with content

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?

Читайте также:  Как узнать название дисплея 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’

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:

Читайте также:  Linux посмотреть все crontab

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.

Источник

How to Find Files Containing Specific Text String in Linux

Almost all file managers for Linux like Nemo and Thunar by default provide an option to search for files. But if you want to search for a string inside a file’s content using file manager, the majority of them do not let you do so.

In this article, I’ll discuss different ways that you can use on any Linux distribution to find all files containing specific text strings or words by recursively digging through all sub-directories.

On this page

Search Specific Text in Linux Using Catfish GUI Tool

I’ll start with the easiest way that can work for all including beginners to advance Linux users. Catfish is a simple and lightweight GUI-based file search tool for Linux desktops. Along with searching for files on your system, you can also use it to find all files that contain a particular word.

Install Catfish Search Tool on Linux

The Catfish package is already available on the primary Debian and Ubuntu repositories. Hence, If you’re using Debian or Ubuntu-based distributions, you can simply install it by running the command:

$ sudo apt install catfish [On Debian/Ubuntu & Mint]

On other Linux distributions, you can install it from the default repositories using your package manager.

$ sudo yum install catfish [On CentOS/RHEL 7] $ sudo dnf install catfish [On CentOS/RHEL 8 & Fedora] $ sudo pacman -S catfish [On Arch Linux] $ sudo pkg_add -v catfish [On FreeBSD]

If the package is not available, you can download the latest release file, extract the downloaded tar.bz2 file, and run the following command:

$ sudo python3 setup.py install

Search File Contents Using Catfish

Once installed, you only need to do is enable “Search file contents”, select directories from the top-left dropdown option, and type the text you want to search for. It will list down all files containing text along with file size and location.

Читайте также:  Linux open oracle port

Search Specific Text in Linux Using Catfish Tool

You can also use file type and modified date filter from the left panel to reduce the search scope. You can even add a directory path where you don’t want to search.

Search Text Based on File Types

Search Specific Text in Linux Using Grep Command

Beside the GUI way, grep is one of the popular command line tools that can be used to search inside file content. Since almost all unix-like operating systems ship grep utility by default, you don’t need to install it.

Search for Particular Text in Files

Now to search and find all files for a given text string in a Linux terminal, you can run the following command. Here, the ‘-r’ or ‘-R’ flag recursively searches through the all subdirectories inside the specified directory.

$ grep -r “linuxshelltips” /home/sarvottam/

Search Specific Text in Files

Find File Names That Contains a Given String

If you want to print only file names and hide the text from the output, you can use the ‘-l’ flag.

$ grep -rl “linuxshelltips” /home/sarvottam/

List File Names Contains Specific Text in Linux

Furthermore, you can also tweak the output using the following options available for grep:

$ sudo grep -rnwi “linuxshelltips” /home/sarvottam/

Search Text and Print File Names and Line Numbers

It is also worth mentioning that if you search through directories that require root permissions, you need to use the sudo command.

Search for Specific Text in Specific File Types

To further reduce the search scope, you can also specifically mention the type of file and directories to only look for while searching. “—include” , “—exclude” , and “—exclude-dir” are the options available to add file type and directory filter.

$ grep --include=\*.txt --exclude=\*. --exclude-dir=\bin -rnwi "linuxshelltips" /home/sarvottam/

Search for Specific Text in Specific File Types

Search Specific Text In Linux Using MC (Midnight Commander)

If you live in a terminal and still want to search text using GUI way, you can also try Terminal User Interface (TUI) based file manager tool, mc (midnight commander) – is a visual file manager that is used to search for files.

Midnight Commander

Install Midnight Commander on Linux

The Midnight Commander is available to install from the default repositories in the most of the Linux distributions.

$ sudo apt install mc [On Debian/Ubuntu & Mint] $ sudo yum install mc [On CentOS/RHEL 7] $ sudo dnf install mc [On CentOS/RHEL 8 & Fedora] $ sudo pacman -S mc [On Arch Linux] $ sudo pkg_add -v mc [On FreeBSD]

Search File Contents Using Midnigh Commander

Inside the terminal, mc provides a visual representation of the filesystem in which you can navigate either through keyboard or mouse. To search file content, you can open a search dialog using ALT+SHIFT+? and enter the text in the “Content:” section.

Search File Contents Using Midnight Commander

As you can see in the above picture, you can also use filters like ignore case, whole words, and regular expression by just checking and unchecking it.

Search File Contents Using Filters

Conclusion

All the above-mentioned applications are beginner’s friendly, free-to-use and open source to add your own enhancements. Alternatively, you can also try other free tools like Ack, The Silver Searcher, Ripgrep, and find command.

Источник

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