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:
Grep "word" File Name
grep root /etc/*
Sample output:
/etc/bash.bashrc:See"man sudo_root"for details.
/etc/crontab:17**** root cd/&& run-parts --report /etc/cron.hourly
/etc/crontab:256*** root test-x /usr/sbin/anacron ||(cd/&& run-parts --report /etc/cron.daily )
/etc/crontab:476**7 root test-x /usr/sbin/anacron ||(cd/&& run-parts --report /etc/cron.weekly )
/etc/crontab:5261** root test-x /usr/sbin/anacron ||(cd/&& run-parts --report /etc/cron.monthly )
/etc/group:root:x:0:
grep:/etc/gshadow:Permission denied
/etc/logrotate.conf: create 0664 root utmp
/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:
grep-l "string" filename
grep-l root /etc/*
Sample output:
/etc/aliases
/etc/arpwatch.conf
grep:/etc/at.deny:Permission denied
/etc/bash.bashrc
/etc/bash_completion
/etc/ca-certificates.conf
/etc/crontab
/etc/group
You can also reverse the output. Use the-L option to output the file names of those unmatched files:
grep-L "word" filename
grep-L root /etc/*
Sample output:
/etc/apm
/etc/apparmor
/etc/apparmor.d
/etc/apport
/etc/apt
/etc/avahi
/etc/bash_completion.d
/etc/bindresvport.blacklist
/etc/blkid.conf
/etc/bluetooth
/etc/bogofilter.cf
/etc/bonobo-activation
/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: