Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-05-2010, 07:35 PM   #1
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Rep: Reputation: 15
Listing files whose contains contain a string, and are of a particular extension?

I believe that the following can be used to locate files that contain the contents 'SomeString' or 'SomeOtherString':
find /home/username/public_html -type f -print0 | xargs -0 egrep -l 'SomeString|SomeOtherString'
How would I limit this to searching for the text 'SomeString' or 'SomeOtherString', but only if the file has extension .php, .inc or .js?

Also - can someone help explain details of what piping to xargs does here? I don't understand how this command actually works.

Last edited by SirTristan; 05-05-2010 at 07:43 PM.
Old 05-05-2010, 08:00 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
ls <pathname> | egrep '(php|inc|js)$' | xargs <...etc.(your code)..>

ls -R if you need it to drill down
Old 05-05-2010, 08:17 PM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
find /home/username/public_html -type f -name '*.php' -o -name '*.inc' -o -name '*.js' -print0 | xargs -0 egrep -l 'SomeString|SomeOtherString'


find /home/username/public_html -type f -name '*.php' -o -name '*.inc' -o -name '*.js' -exec egrep -l 'SomeString|SomeOtherString' {} \;
Old 05-06-2010, 02:10 AM   #4
sumeet inani
Registered: Oct 2008
Posts: 902
Blog Entries: 26

Rep: Reputation: 49
When you
run a command then add a "| xargs command2". The results of command1 will be passed to
command2, possibly on a line−by−line basis

-print0 means null char after file name not newline as we have also given -0 option for xargs.
-l means only filename mentioned not matching content within it.
egrep means extended pattern in grep (same as grep -E)

i suggest
find . -type f | grep -i "file_extension_you_want" | egrep -i 'pattern1|pattern2'
Though not short , but more lucid.
by the way -i means ignore case

Last edited by sumeet inani; 05-06-2010 at 02:14 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Listing files jdnow09 Linux - Newbie 2 08-31-2009 04:21 PM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-05-2008 12:59 AM
Help with listing files sandeshsk007 Linux - Software 10 12-07-2007 05:04 PM
listing files and burning files - just a question radiodee1 Debian 1 11-26-2006 08:29 AM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 12:42 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:23 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration