Find Files Containing Specific Patterns in Linux

Written by: Bobbin Zachariah   |   Last updated: August 3, 2023

In a Linux environment, finding files containing a specific pattern is an essential task for managing and organizing large quantities of data.

Find Files Containing Specific Patterns

Utilizing tools like grep, ripgrep, and find we can efficiently find files where the specific string is located.

Using Grep

By default when grep performs a recursive search using -r option it can display filenames along with matching lines.

Example:

grep -r dir1/ -e Linux
find files that match a specific pattern using grep recursively

This command will output the lines in the files where the string "Linux" is found, along with the file name.

If you like to display only the matching file names without showing the matching lines add -l option.

grep -rl dir1/ -e Linux
find files for a specific pattern match and output only its filenames using grep with -l

To customize your output you can consider options such as -i (ignore case), -w (exact word match) and -n (to display the line numbers of the match).

You may also use --include and --exclude option with grep to filter search to specific file types. Example:

grep -r dir1/ -e Linux --include \*.txt
find files that match a pattern from specific file types .txt

This will return the file names and the line numbers where the pattern "Linux" is found in all .txt files in the dir1 directory.

Using ripgrep

Using ripgrep (rg), to find files for matching patterns is easier than grep. You don't have to provide any option by default rg recursively searches the directory and lists match with respective filenames at the top.

Example:

rg Linux dir1/
using ripgrep find files that match a pattern

When compared to grep we have not provided any option. The output shows the path to the files where the string match was found, line numbers where the match was found along with actual lines from the files that contain the string.

Similar to grep you can use -i, -w, and -l option with ripgrep with the same functions.

Using find command

The find command is primarily used for searching files and doesn't search within the contents of files by itself. But you can combine it with grep to find files containing a specific pattern, this helps allows for very precise control over which files are searched.

Example:

find /home/ubuntu/dir1/ -name "*.[tT][xX][tT]" -print | xargs grep -n "Linux"
using find with grep to find files that match a pattern.

This command search for the pattern "Linux" in all .txt and .TXT files within the specified directory /home/ubuntu/dir1/. Each line of the output will include the path to a file where "Linux" was found, the line number where it was found, and the actual text of the line containing "Linux."

Note: Here xargs command takes the list of files from find and passes them as arguments to grep and -print option outputs the names of matching files.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment