Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux grep and file filter

Posted on 2014-11-17
2
Medium Priority
?
370 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 1000 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

886 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