Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the grep command to search for file names in Linux

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the grep command to search for file names in Linux. The content of the article is of high quality, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.

Search and display file names from files

When you search from more than one file, by default it will display the filename:

grep "word" filename grep root /etc/*

Example output:

/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 part at the beginning of each line is the file name (e.g./etc/crontab,/etc/group). Use the-l option to display only file names:

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

Example output:

/etc/aliases/etc/arpwatch.confgrep: /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 files that don't match:

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

Example 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

Find files by file content

Enter the following command:

grep 'string' *.txtgrep 'main(' *.cgrep '#include' *.cgrep 'getChar*' *.cgrep -i 'ultra' *.confgrep -iR 'ultra' *.conf

wherein

-i: Ignore case of pattern (matches string valid, VALID, VALID) and input file (matches file.c FILE.c FILE.C).

-R: Recursively reads all files in each directory.

Highlight matched patterns

You can easily highlight modes when searching for a large number of files:

$ grep --color=auto -iR 'getChar();' *.c

Displays file names and line numbers for patterns found

You may want to display file names and line numbers:

$ grep --color=auto -iRnH 'getChar();' *.c

Among them,

-n: Prefaces each line in the output with a line number starting with 1.

-H: Print the file name for each match. This is the default option when searching multiple files. (The-h option forces file names to be hidden; the-l and-L options are used to display only matching/non-matching file names, while the-H and-h options control the display/non-display of file names before matching lines are displayed.)

$grep --color=auto -nH 'DIR' *

Sample output:

You can also use the find command:

The code is as follows:

$ find . -name "*.c" -print |xargs grep "main(" How to use grep command to search file names in Linux is shared here. I hope the above content can be of some help to everyone and learn more. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report