Grep command series: Search from files and display file names

Source: Internet
Author: User

Grep command series: Search from files and display file names

How can I search for (grep) from several files and display only the names of matched files?

When you search for more than one file, the file name is displayed by default:

  1. Grep "word" File Name
  2. grep root /etc/*

Sample output:

  1. /etc/bash.bashrc:See"man sudo_root"for details.
  2. /etc/crontab:17**** root cd/&& run-parts --report /etc/cron.hourly
  3. /etc/crontab:256*** root test-x /usr/sbin/anacron ||(cd/&& run-parts --report /etc/cron.daily )
  4. /etc/crontab:476**7 root test-x /usr/sbin/anacron ||(cd/&& run-parts --report /etc/cron.weekly )
  5. /etc/crontab:5261** root test-x /usr/sbin/anacron ||(cd/&& run-parts --report /etc/cron.monthly )
  6. /etc/group:root:x:0:
  7. grep:/etc/gshadow:Permission denied
  8. /etc/logrotate.conf: create 0664 root utmp
  9. /etc/logrotate.conf: create 0660 root utmp

The first part of each line is the file name (for example,/etc/crontab and/etc/group ). You can use the-l option to display only the file name:

  1. grep-l "string" filename
  2. grep-l root /etc/*

Sample output:

  1. /etc/aliases
  2. /etc/arpwatch.conf
  3. grep:/etc/at.deny:Permission denied
  4. /etc/bash.bashrc
  5. /etc/bash_completion
  6. /etc/ca-certificates.conf
  7. /etc/crontab
  8. /etc/group

You can also reverse the output. Use the-L option to output the file names of those unmatched files:

  1. grep-L "word" filename
  2. grep-L root /etc/*

Sample output:

  1. /etc/apm
  2. /etc/apparmor
  3. /etc/apparmor.d
  4. /etc/apport
  5. /etc/apt
  6. /etc/avahi
  7. /etc/bash_completion.d
  8. /etc/bindresvport.blacklist
  9. /etc/blkid.conf
  10. /etc/bluetooth
  11. /etc/bogofilter.cf
  12. /etc/bonobo-activation
  13. /etc/brlapi.key

Grep uses concise and Regular Expressions

Regular Expression usage

Assertion with Zero Width of a regular expression

Linux Command-grep for file text operations

Grep Regular Expression

Regular Expressions and file formatting commands in Linux (awk/grep/sed)

Via: http://www.cyberciti.biz/faq/grep-from-files-and-display-the-file-name/

Author: Vivek Gite Translator: wxy Proofreader: wxy

This article was originally compiled by LCTT and launched with the honor of Linux in China

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.