Linux file files containing text

How to find all files containing specific text in Linux

This tutorial will teach you how to recursively search for files containing a specific string on Linux using the command line. This tutorial uses the ‘grep’ command to search for strings in files. Alternatively, you may use the find command to look for files with specific content.

A basic syntax for searching text with grep command:

The grep command offers other useful options for finding specific text in file systems.

  • -r, —recursive : Search files recursively
  • -R, —dereference-recursive : Search files recursively and follow symlinks
  • —include=FILE_PATTERN : search only files that match FILE_PATTERN
  • —exclude=FILE_PATTERN : skip files and directories matching FILE_PATTERN
  • —exclude-from=FILE : skip files matching any file pattern from FILE
  • —exclude-dir=PATTERN : directories that match PATTERN will be skipped.
  • -L, —files-without-match : Print file names containing no match
  • -l, —files-with-matches : Print string containing file names only
  • -i, —ignore-case : ignore case of search string
  • -e, —regexp=PATTERN : Use a pattern to search or specify multiple search strings
  • -w, —word-regexp : force to match whole words

There are several ways to use the grep command to search text. Let’s discuss a few examples of searching a text/string in the file system.

The below example command will search the string “Error” in all files in /var/log directory and its sub-directories.

Search specific text in all files in Linux

The -e switch can also be utilized to find multiple strings. This is comparable to the egrep program. The example below will look for “Error” and “Warning” in all the files in the /var/log directory and its subdirectories.

grep -rlw -e "Error" -e "Warning" /var/log 

Search multiple string in all files in Linux

You can search strings in files that match the file name criteria. The following command searches for “Error” in files with the .log extension in the /var/log directory and its sub-directories.

grep -rlw --include="*.log" -e "Error" /var/log 

You can use the —exclude option in find to exclude some files that match certain file name criteria. For example, you can exclude files with the .txt extension.

grep -rlw --exclude="*.txt" -e "tecadmin" /var/log 

You can also skip searching certain directories. For instance, don’t search for string files in any folder with apache2 in its name.

grep -rlw --exclude-dir="*apache2*" -e "tecadmin" /var/log 

Conclusion

You have learned how to search for specific text in files on the Linux file system in this tutorial.

Читайте также:  Wine for linux manjaro

Источник

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.

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.

Читайте также:  Linux generic x86 support

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.

Источник

Find files containing a given text

In bash I want to return file name (and the path to the file) for every file of type .php|.html|.js containing the case-insensitive string «document.cookie» | «setcookie» How would I do that?

6 Answers 6

egrep -ir --include=*. "(document.cookie|setcookie)" . 

The r flag means to search recursively (search subdirectories). The i flag means case insensitive.

If you just want file names add the l (lowercase L ) flag:

egrep -lir --include=*. "(document.cookie|setcookie)" . 

that didn’t seem to work for me(at least not on mac). just hangs. egrep -lir —include=* «repo» egrep: warning: recursive search of stdin

You forgot to add the path to search. The path is ‘.’ in the above example. In your case, the script is waiting for the input to search on stdin. Try: egrep -lir —include=* «repo» / (or any other path)

Try something like grep -r -n -i —include=»*.html *.php *.js» searchstrinhere .

the -i makes it case insensitlve

Читайте также:  Чем распаковать архив tar linux

the . at the end means you want to start from your current directory, this could be substituted with any directory.

the -r means do this recursively, right down the directory tree

the -n prints the line number for matches.

the —include lets you add file names, extensions. Wildcards accepted

find them and grep for the string:

This will find all files of your 3 types in /starting/path and grep for the regular expression ‘(document\.cookie|setcookie)’ . Split over 2 lines with the backslash just for readability.

find /starting/path -type f -name "*.php" -o -name "*.html" -o -name "*.js" | \ xargs egrep -i '(document\.cookie|setcookie)' 

Thanks @Michael Berkowski This way fastest more than 5 or 8 times # egrep -ir —include=file.foo «(foo|bar)» /dir on ~500Gb weigth directory.

Sounds like a perfect job for grep or perhaps ack

Or this wonderful construction:

find . -type f \( -name *.php -o -name *.html -o -name *.js \) -exec grep "document.cookie\|setcookie" /dev/null <> \; 

@MichaelBerkowski : You can use it like this to deal with whitespace in filenames: find . -type f -print0 | xargs -0 -I <> grep «search_string» <> . Of course, the other options can be added as well.

find . -type f -name '*php' -o -name '*js' -o -name '*html' |\ xargs grep -liE 'document\.cookie|setcookie' 

Just to include one more alternative, you could also use this:

find «/starting/path» -type f -regextype posix-extended -regex «^.*\.(php|html|js)$» -exec grep -EH ‘(document\.cookie|setcookie)’ <> \;

  • -regextype posix-extended tells find what kind of regex to expect
  • -regex «^.*\.(php|html|js)$» tells find the regex itself filenames must match
  • -exec grep -EH ‘(document\.cookie|setcookie)’ <> \; tells find to run the command (with its options and arguments) specified between the -exec option and the \; for each file it finds, where <> represents where the file path goes in this command. while
    • E option tells grep to use extended regex (to support the parentheses) and.
    • H option tells grep to print file paths before the matches.

    And, given this, if you only want file paths, you may use:

    find «/starting/path» -type f -regextype posix-extended -regex «^.*\.(php|html|js)$» -exec grep -EH ‘(document\.cookie|setcookie)’ <> \; | sed -r ‘s/(^.*):.*$/\1/’ | sort -u

    • | [pipe] send the output of find to the next command after this (which is sed , then sort )
    • r option tells sed to use extended regex.
    • s/HI/BYE/ tells sed to replace every First occurrence (per line) of «HI» with «BYE» and.
    • s/(^.*):.*$/\1/ tells it to replace the regex (^.*):.*$ (meaning a group [stuff enclosed by () ] including everything [ .* = one or more of any-character] from the beginning of the line [ ^ ] till’ the first ‘:’ followed by anything till’ the end of line [ $ ]) by the first group [ \1 ] of the replaced regex.
    • u tells sort to remove duplicate entries (take sort -u as optional).

    . FAR from being the most elegant way. As I said, my intention is to increase the range of possibilities (and also to give more complete explanations on some tools you could use).

    Источник

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