Getting Started with Terminal: 4 Ways to Find a Word In a File


By default, most search tools look at file names, not file contents. However, the most famous GNU search program grep will look inside files with the correct flags. Here we will show you how you can find specific word(s) in a file on Linux.

Using grep to Find a Word In a File

By default, grep searches through the contents of files as well as their file name. It’s included on the largest number of Linux systems, and is generally identical across distros. That said, smaller or less powerful Linux boxes might prefer to run a different command, like ack.

Depending on how the file is encoded, grep may not always be able to look inside. But for most text-based formats, grep can scan the text of the file for the specified pattern.

grep -Rw '/path/to/search/' -e 'pattern'

The -R flag sets grep to recursive mode, navigating through all the directories contained within the specified directory. The -w flag searches for whole word matches. This means that ‘red’ will match only ‘red’ surrounded by whitespace characters, and not ‘redundant’ or ‘tired’. The -e flag prefaces the pattern to search for. It supports regular expressions by default.

To speed up grep, you can use the –exclude and –include flags to limit the search to certain types of files. For example, –exclude=*.csv will not search within any files with the .csv extension. –include=*.txt, on the other hand, will only search within files with the .txt extension. The flag can be added immediately after the grep command, like so:

grep --exclude=*.csv -Rw '/path/to/search' -e 'pattern'

You can also exclude specified directories by following the format below:

grep --exclude-dir={dir1,dir2,*_old} -Rw '/path/to/search' -e 'pattern'

This command will not search in any directories in the present working directory named dir1, dir2, or matching the pattern *_old, eliminating them from the search process. It will execute the specified recursive, full word match search on all other files in the present working directory.

For complete review, check out grep’s man page.

Using find to Find a Specific Word In a File

 

While the find command’s syntax is more complicated that grep, some prefer it.

find . -name "*.php" -exec grep "pattern" {} \;

This command would use find’s -exec flag to pass the found files to grep for searching. With a clever arrangement of syntax, you can use find’s faster file-system search to locate the specific file types you want to search within, then pipe them to grep in order to search inside the files.

Note that find only looks at filenames, not contents. That’s why grep is required to search file text and contents. The normal grep flags should be fully operational from within the -exec flag.

For complete review, check out find’s man page.

Using ack to Find a Specific Word In a File

The ack command is likely the fastest searching tool, but it’s not as popular as the above options.

Ack does require pre-installation with Homebrew. If you already have Hombrew installed, you can run the following command to install the most up-to-date version of ack on your Mac.

brew install ack

Once you have ack installed, you’ll find its pattern matching to be quite simple. For example, the command below will search within the current directory.

ack 'pattern'

If you want to search within a specific file or directory, you can append that file or fully-qualified pathname to your search.

ack 'pattern' /path/to/file.txt

For additional flags and options, check out ack’s documentation.

Using RipGrep to Find a Word In a File

For most folks in most situations, grep is the best widely-available search tool. RipGrep aims to offer faster searching options, combining the power of grep with the speed of ack. You can install RipGrep from GitHub or through Homebrew.

The basic syntax is similar. The command below will search recursively in the present working directory for any files that match the provided pattern.

rg pattern

For additional flags and options, review RipGrep’s documentation.

You might also like the following posts:

What Makes Private Email Servers Worth Paying For?

Pro Terminal Commands: 10 Uses for ps on macOS

Pro Terminal Commands: Use fd on macOS to Find Files


Alexander Fox

0 Comments

Your email address will not be published.