×
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):

1

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

2

Choose the image Ubuntu 20.04:

3

Continue with the configuration of your instance until the end:

4

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/group-:40:systemd-journal:x:101:
/etc/group-:41:systemd-network:x:102:
/etc/group-:42:systemd-resolve:x:103:
/etc/group-:43:systemd-timesync:x:104:
/etc/group-:55:systemd-coredump:x:999:
/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
etc/group-
/etc/default/networkd-dispatcher
/etc/default/irqbalance
/etc/default/rsync
/etc/default/chrony
/etc/cron.daily/apt-compat
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
....
....

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/ 
/etc/group-
/etc/default/networkd-dispatcher
/etc/default/irqbalance
/etc/default/rsync
/etc/default/chrony
/etc/cron.daily/apt-compat
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
/etc/apparmor.d/usr.sbin.rsyslogd
/etc/apparmor.d/abstractions/libpam-systemd
/etc/apparmor.d/abstractions/nameservice
/etc/apparmor.d/abstractions/base
/etc/apparmor.d/abstractions/dbus-session-strict
....
....

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" {} \;
/etc/rpc
/etc/default/nss
/etc/cloud/cloud.cfg
/etc/apparmor.d/abstractions/authentication
/etc/apparmor.d/abstractions/ubuntu-browsers.d/java
/etc/apparmor.d/abstractions/nameservice
/etc/pam.d/chfn
/etc/pam.d/passwd
/etc/pam.d/su
0 0 0
Share on

Alibaba Cloud Community

1,076 posts | 263 followers

You may also like

Comments