Community Blog How Do I Find All Files Containing Specific Text on Linux?

How Do I Find All Files Containing Specific Text on Linux?

This article explains how to find any file containing specific text on Linux.

By Alain Francois

You have probably faced a situation where you are looking for a file, but you don't remember its name; you only remember the contents. It is possible to retrieve the file if you remember specific text within the file. Linux systems offer some commands that can help you to do so.

Run Your Linux Server

The following step-by-step guide explains how to run your MySQL server from the Alibaba Cloud console.

Log in to your Alibaba Cloud account and go to Elastic Compute Service (ECS):


Create a new instance. We will choose a Pay-As-You-Go instance:


Choose the image Ubuntu 20.04:


Continue with the configuration of your instance until the end:


How Do I Find All Files Containing Specific Text?

The following Linux commands explain how to find files containing specific text.

Grep Command

grep is a built-in Linux command that prints lines that match a given pattern. It returns all the lines of a file that contain a certain string by default, and the command is also case-sensitive. grep offers some interesting. You can tell the command to search in subdirectories, ignore the case, and more, using specific parameters.

  • i to ignore the case
  • r for a recursive search
  • l to only print the names of the file containing the pattern lines
  • n shows the line number containing the pattern

The syntax is listed below:

grep -ri "pattern" /directory-path

You can try to look for files with the "systemd" pattern:

$ sudo grep -rni "systemd" /etc/
/etc/default/networkd-dispatcher:2:# by the included systemd service file.
/etc/default/irqbalance:3:# seconds.  This is the environment file that is specified to systemd via the 
/etc/default/rsync:4:# If this system uses systemd, you can specify options etc. for rsync
/etc/default/rsync:5:# in daemon mode by copying /lib/systemd/system/rsync.service to
/etc/default/rsync:6:# /etc/systemd/system/rsync.service and modifying the copy; add required
/etc/default/chrony:2:# /lib/systemd/system/chrony.service; it allows you to pass various options to

The line number can help you. Then, you can filter the result to only have the filename with awk and remove duplicated filename:

$ sudo grep -rni "systemd" /etc/ | awk -F: '{print $1}' | uniq

It must be easier to filter the filename by using the -l parameter since it will remove the duplicated file automatically:

$ sudo grep -lri "systemd" /etc/ 

If you want to search in the current directory, you only have to omit the path:

$ grep -ri "systemd"

You can use the -w parameter if you only want to display lines that match the whole word.

Find Command

You can also use the find command that you can combine with the grep command:

find /directory-path -type f -exec grep -l "pattern" {} \;

This command gives you output without a duplicated file:

$ sudo find /etc -type f -exec grep -l "passwd" {} \;
0 0 0
Share on

Alibaba Cloud Community

632 posts | 114 followers

You may also like


Alibaba Cloud Community

632 posts | 114 followers

Related Products

  • Cloud Shell

    A Web browser-based admin tool that allows you to use command line tools to manage Alibaba Cloud resources.

    Learn More
  • Red Hat Enterprise Linux

    Take advantage of the cost effectiveness, scalability, and flexibility of Alibaba Cloud's infrastructure and services, as well as the proven reliability of Red Hat Enterprise Linux and Alibaba Cloud's support backed by Red Hat Global Support Services.

    Learn More
  • Server Guard

    An easy to use service that provides real-time monitoring of servers to ensure high availability

    Learn More
  • Database Overview

    Fully managed and less trouble database services

    Learn More