Solved

Linux grep and file filter

Posted on 2014-11-17
2
299 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
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…
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now