Solved

find files with specific content on linux system

Posted on 2011-03-18
7
315 Views
Last Modified: 2012-05-11
hi

i need a bash command to find files

1) all .php files in a directory and all subdirectories containing a specific search string
2) all .php files in a directory and all subdirectories  belonging to a specific user and group
3)  all .php files in a directory and all subdirectories containing a specific search string and owned by a specific user and group

Thanks
0
Comment
Question by:netslt
  • 4
  • 2
7 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35166825
find /directory -type f -name "*.php" | xargs grep -l "search string"

find /directory -type f -name "*.php" -user userid -group groupid

find /directory -type f -name "*.php" -user userid -group groupid  | xargs grep -l "search string"

0
 

Author Comment

by:netslt
ID: 35166885
great!

 just another thing that came to my mind: would it also be possible to search for two or more strings, eg "containing string 1 OR string 2 OR string 3"

and would it be possible to output the line from the file where the string was found?

thanks!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35166900
find /directory -type f -name "*.php" | xargs egrep -l "string1|string2|string3"

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 35166949
>> output the line <<

find /directory -type f -name "*.php" | xargs egrep "string1|string2|string3" /dev/null

just without "-l"
/dev/null at the end is to display the filename even if only one file is found. No "|" or ">" in front of it!

wmp

0
 

Author Closing Comment

by:netslt
ID: 35167065
grossartig wie immer - vielen dank!
0
 
LVL 3

Expert Comment

by:Alberto Cortes
ID: 35167108
Solution:

find /directory/path -name "*string*.php" -print 2> /dev/null
find /directory/path -name "*string*.php" -group <group_name> -user <user_name> -print 2> /dev/null
find /directory/path -group <group_name> -user <user_name> -print 2>/dev/null | xargs grep "string"
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35168563
Danke fuer die Punkte und viele Gruesse in die schoene Schweiz!

wmp
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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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.

746 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

18 Experts available now in Live!

Get 1:1 Help Now