Solved

Linux grep and file filter

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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.…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

839 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