TranslateProject/sources/tech/Linux or UNIX grep Command Tutorial series/20151127 Linux or UNIX grep Command Tutorial series 5--Grep From Files and Display the File Name.md
2015-12-21 09:13:09 +08:00

2.2 KiB
Raw Blame History

(translating by runningwater) Grep From Files and Display the File Name

How do I grep from a number of files and display the file name only?

When there is more than one file to search it will display file name by default:

grep "word" filename
grep root /etc/*

Sample outputs:

/etc/bash.bashrc:       See "man sudo_root" for details.
/etc/crontab:17 *       * * *   root    cd / && run-parts --report /etc/cron.hourly
/etc/crontab:25 6       * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
/etc/crontab:47 6       * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
/etc/crontab:52 6       1 * *   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 name is file name (e.g., /etc/crontab, /etc/group). The -l option will only print filename if th

grep -l "string" filename
grep -l root /etc/*

Sample outputs:

/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 suppress normal output; instead print the name of each input file from which no output would normally have been printed:

grep -L "word" filename
grep -L root /etc/*

Sample outputs:

/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

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

作者Vivek Gite 译者:runningwater 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出