Solved

find files with specific content on linux system

Posted on 2011-03-18
7
317 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…

785 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