Solved

Linux grep and file filter

Posted on 2014-11-17
2
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

733 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