How to Use the Linux grep command to find all files with specific text

Source: Internet
Author: User

How to Use the Linux grep command to find all files with specific text
Purpose

This article provides useful tips on how to find a specific directory or all files containing a specific word or string in a Linux File System.

Conventions
  • #-Directly run the given command as the root user or using the sudo command as the root user
  • $-The given command is executed as a common non-privileged user
In this example, all files with specific strings are searched in non-recursive mode.

The first command example will be/etc/Search for strings in all files in the directoryartfulAnd exclude any subdirectories:

Bkjia @ bkjia :~ $ Grep-s artful/etc /*
/Etc/lsb-release: DISTRIB_CODENAME = artful
/Etc/OS-release: VERSION_CODENAME = artful
/Etc/OS-release: Ubuntu_CODENAME = artful

The grep-s option will disable error messages about non-existing or unreadable files. The output displays the file name and the actual row containing the request string.

For example:

 

Recursively searches for all files with specific strings

The preceding commands omit all subdirectories. Recursive search means to traverse all subdirectories. The following command searches for strings in all files (including all subdirectories) in the/etc/directory:artful:

Bkjia @ bkjia :~ $ Sudo grep-R artful/etc /*
[Sudo] bkjia password:
/Etc/apt/sources. list. save: # deb cdrom: [Ubuntu 17.10 _ Artful javasdvark _-Release amd64 (20171018)]/artful main restricted
/Etc/apt/sources. list. save: deb http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/Etc/apt/sources. list. save: # deb-src http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/Etc/apt/sources. list. save: deb http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/Etc/apt/sources. list. save: # deb-src http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/Etc/apt/sources. list. save: deb http://cn.archive.ubuntu.com/ubuntu/ artful universe
/Etc/apt/sources. list. save: # deb-src http://cn.archive.ubuntu.com/ubuntu/ artful universe
/Etc/apt/sources. list. save: deb http://cn.archive.ubuntu.com/ubuntu/ artful-updates universe
......
/Etc/dictionaries-common/words: artful
/Etc/dictionaries-common/words: artfully
/Etc/dictionaries-common/words: artfulness
/Etc/dictionaries-common/words: artfulness's
/Etc/lsb-release: DISTRIB_CODENAME = artful
/Etc/OS-release: VERSION_CODENAME = artful
/Etc/OS-release: UBUNTU_CODENAME = artful

For example

Search for all files containing specific words

The preceding grep command example lists the contained strings.artfulAll files. It also showsartfules,artfuled. Use the grep-w option to display only one specific word:

Bkjia @ bkjia :~ $ Sudo grep-Rw artful/etc /*
[Sudo] bkjia password:
/Etc/apt/sources. list. save: # deb cdrom: [Ubuntu 17.10 _ Artful javasdvark _-Release amd64 (20171018)]/artful main restricted
/Etc/apt/sources. list. save: deb http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/Etc/apt/sources. list. save: # deb-src http://cn.archive.ubuntu.com/ubuntu/ artful main restricted
/Etc/apt/sources. list. save: deb http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/Etc/apt/sources. list. save: # deb-src http://cn.archive.ubuntu.com/ubuntu/ artful-updates main restricted
/Etc/apt/sources. list. save: deb http://cn.archive.ubuntu.com/ubuntu/ artful universe
......
/Etc/dictionaries-common/words: artful
/Etc/lsb-release: DISTRIB_CODENAME = artful
/Etc/OS-release: VERSION_CODENAME = artful
/Etc/OS-release: UBUNTU_CODENAME = artful

For example:

Lists only the names of objects containing specific text.

The preceding command may produce unnecessary output. In the next example, only strings in the/etc/directory are displayed recursively.artfulAll file names:

Bkjia @ bkjia :~ $ Sudo grep-Rl artful/etc /*
/Etc/apt/sources. list. save
/Etc/apt/sources. list
/Etc/apt/sources. list. d/noobslab-ubuntu-deepin-sc-artful.list
/Etc/apt/sources. list. d/openshot_developers-ubuntu-ppa-artful.list
/Etc/apt/sources. list. d/forkotov02-ubuntu-ppa-artful.list
/Etc/apt/sources. list. d/forkotov02-ubuntu-ppa-artful.list.save
/Etc/apt/sources. list. d/openshot_developers-ubuntu-ppa-artful.list.save
/Etc/dictionaries-common/words
/Etc/lsb-release
/Etc/OS-release

For example:

Perform a case-insensitive search

All searches are case-sensitive by default. This means that any search string will only display the exact matching files in both upper and lower case. By using the-I option of grep, this command also lists any rows that contain Artful, STRETCH, and ArtFul, so as to execute a case-insensitive search.

Bkjia @ bkjia :~ $ Sudo grep-Ril artful/etc /*
/Etc/apt/sources. list. save
/Etc/apt/sources. list
/Etc/apt/sources. list. d/noobslab-ubuntu-deepin-sc-artful.list
/Etc/apt/sources. list. d/openshot_developers-ubuntu-ppa-artful.list
/Etc/apt/sources. list. d/forkotov02-ubuntu-ppa-artful.list
/Etc/apt/sources. list. d/forkotov02-ubuntu-ppa-artful.list.save
/Etc/apt/sources. list. d/openshot_developers-ubuntu-ppa-artful.list.save
/Etc/dictionaries-common/words
/Etc/lsb-release
/Etc/OS-release

For example:

Include or exclude specific file names in the search

The grep command can also contain only specific files as part of the search. For example, we only want to search for specific text/strings in the configuration file with the extension. conf. In the next example, all files with the. conf extension containing the string bash will be found in the/etc directory:

Bkjia @ bkjia :~ $ Sudo grep-Ril bash/etc/*. conf
/Etc/adduser. conf

Or

Bkjia @ bkjia :~ $ Sudo grep-Ril -- include = \ *. conf bash/etc /*
/Etc/adduser. conf

Similarly, using the -- exclude option, we can exclude any specific file name:

Bkjia @ bkjia :~ $ Sudo grep-Ril -- exclude = \ *. conf bash/etc /*
/Etc/alternatives/lzcmp
/Etc/alternatives/vivaldi
/Etc/alternatives/vi
/Etc/alternatives/view
/Etc/alternatives/gnome-www-browser
/Etc/alternatives/ex
/Etc/alternatives/x-www-browser
/Etc/alternatives/lzdiff
/Etc/alternatives/rview
/Etc/alternatives/x-session-manager
/Etc/apparmor/init/network-interface-security/sbin. dhclient
/Etc/apparmor. d/sbin. dhclient
/Etc/apparmor. d/usr. sbin. cupsd
/Etc/apparmor. d/usr. lib. libreofficeprogram. senddoc
/Etc/apparmor. d/usr. bin. evince
/Etc/apparmor. d/export actions/ubuntu-browsers.d/plugins-common
/Etc/apparmor. d/abstractions/bash
/Etc/apparmor. d/export actions/private-files
/Etc/apparmor. d/usr. lib. libreofficeprogram. soffice. bin
/Etc/bash. bashrc
/Etc/bash_completion
/Etc/bash_completion.d/grub
/Etc/bash_completion.d/git-prompt
/Etc/bash_completion.d/apport_completion
/Etc/cron. daily/mlocate
/Etc/cron. daily/apt-compat
/Etc/dhcp/dhclient-enter-hooks.d/resolved
/Etc/dictionaries-common/words
/Etc/gdm3/Xsession
/Etc/group
/Etc/group-
/Etc/gshadow
/Etc/gshadow-
/Etc/ImageMagick-6/mime. xml
/Etc/inputrc
/Etc/passwd
/Etc/passwd-
/Etc/profile
/Etc/profile. d/bash_completion.sh
/Etc/profile. d/vte-2.91.sh
/Etc/shells
/Etc/skel/. bashrc
/Etc/skel/. bash_logout
/Etc/skel/. profile

For example:

Exclude specific directories from search

Like a file, grep can also exclude a specific directory from the search. Use the -- exclude-dir option to exclude directories from search. The following search example finds the contained string in the/etc directoryartfulAnd exclude/etc/grub. d from the search:

Bkjia @ bkjia :~ $ Sudo grep -- exclude-dir =/etc/grub. d-Rwl artful/etc /*
/Etc/apt/sources. list. save
/Etc/apt/sources. list
/Etc/apt/sources. list. d/noobslab-ubuntu-deepin-sc-artful.list
/Etc/apt/sources. list. d/openshot_developers-ubuntu-ppa-artful.list
/Etc/apt/sources. list. d/forkotov02-ubuntu-ppa-artful.list
/Etc/apt/sources. list. d/forkotov02-ubuntu-ppa-artful.list.save
/Etc/apt/sources. list. d/openshot_developers-ubuntu-ppa-artful.list.save
/Etc/dictionaries-common/words
/Etc/lsb-release
/Etc/OS-release

Display the row number containing the search string

By using the-n option, grep also provides information about the row number of a specific string:

Bkjia @ bkjia :~ $ Sudo grep-Rni bash/etc/*. conf
/Etc/adduser. conf: 6: DSHELL =/bin/bash

Find all files that do not contain a specific string

The last example uses the-v option to list all files that do not contain specific keywords. For example, the following search will list strings that do not containartfulAll files in the/etc/directory:

Bkjia @ bkjia :~ $ Sudo grep-Rlv artful/etc /*
/Etc/acpi/events/asus-wireless-on
/Etc/acpi/events/lenovo-undock
/Etc/acpi/events/ibm-wireless
/Etc/acpi/events/thinkpad-cmos
/Etc/acpi/events/asus-wireless-off
/Etc/acpi/events/asus-keyboard-backlight-up
/Etc/acpi/events/asus-keyboard-backlight-down
......

Is this article useful to you? You are welcome to leave a message in the comments section under bkjia.com to let us know your thoughts.

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.