Solved

Linux grep and file filter

Posted on 2014-11-17
2
309 Views
Last Modified: 2014-11-17
I can find all files that contain 8585 in the following command (inlcuing files within subdirectories)

grep -R "8585" .

How can I search for 8686, but only for files that end in .txt (in other words *.txt) ?

Thanks
0
Comment
Question by:Anthony Lucia
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 40447999
find . -type f -name "*.txt" | xargs grep "8585"

Why we have to use "find"?

When "grep" is used with "-R" the file parameter must be a directory ("." in this case), so there is no way of directly applying a filename filter.
"find" works recursively (as "grep -R" does),  has the option of filename filtering, but doesn't have a content filter (which "grep" has). So we must use "find" and "grep".
0
 
LVL 23

Assisted Solution

by:savone
savone earned 250 total points
ID: 40448344
You can specify the file extension with grep.

grep -r --include "*.txt" 8686 .

or

grep -r "8686" *.txt .
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now