Linux find file containing string

Grep to find a file that contains a string in a directory

I’m trying to get familiar with the basics of the terminal. I’d like to find the file within my CMS website that contains my Google Analytics tracking code «gaq» as a string to search should do the trick. There is a folder on my desktop that contains all of the sites files.

grep gaq /Users/myname/Desktop/website grep gaq * /Users/myname/Desktop/website 

I searched on SO and Google but the internet seems crowded out with slightly more advanced uses of grep involving regular expressions and conditions. e.g.: Unix Command to List files containing string but *NOT* containing another string, How can I use grep to find a word inside a folder?. I thought I’d found the answer with the second example question. I tried the following command: grep -nr gaq* /Users/myname/Desktop/website But that returned many results and, from what I can see, not exactly accurate matches of my search string. Here’s a sample of the Google Analytics snippet, taken by viewing the source of the html page. My goal is to find the file that generates the analytics snippet in order to update it to the newer version of Google Analytics:

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']); _gaq.push(['_trackPageview']); 

So I am using «gaq» as a string to search for. I realize that this must sound pretty basic but it’s pretty frustrating as a beginner to the shell. How would I search the directory /Users/myname/Desktop/website for the file (return the file not the actual paragraph of text) that contains the analytics code using grep (assuming grep is the command I should be using?)

Источник

Читайте также:  Pop os linux kde

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.

Читайте также:  Настройка точки доступа wifi linux

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.

Читайте также:  Создать ярлыки linux mint

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.

Источник

How to find lines containing a string in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I have a file in Linux, I would like to display lines which contain a specific string in that file, how to do this?

5 Answers 5

The usual way to do this is with grep , which uses a regex pattern to match lines:

Each line which matches the pattern will be output. If you want to search for fixed strings only, use grep -F ‘pattern’ file . fgrep is shorthand for grep -F .

addition grep -rn ‘string’ /path/ if you want to search a string in a folder in which file including and line number

Besides grep , you can also use other utilities such as awk or sed

Here is a few examples. Let say you want to search for a string is in the file named GPL .

Your sample file

$ cat -n GPL 1 The GNU General Public License is a free, copyleft license for 2 The licenses for most software and other practical works are designed 3 the GNU General Public License is intended to guarantee your freedom to 4 GNU General Public License for most of our software; 
$ grep is GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to 
$ awk /is/ GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to 
$ sed -n '/is/p' GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to 

Источник

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