Need help finding file contents in Linux

Posted on 2010-04-02
Medium Priority
Last Modified: 2013-12-16
I'm having trouble with the find/grep syntax to find files that contain a certain string.

I've got a CentOS Linux server and when I do the first command command, I get a list of a whole bunch of files as expected. But when I do the second command, I get nothing.
find ./home/mark/domains/markgoodall.com/public_html/front_end_code -name *.php -print0
find ./home/mark/domains/markgoodall.com/public_html/front_end_code -name *.php -print0 | xargs grep '<head>'

Open in new window

Question by:markgoodall

Expert Comment

ID: 29464465

Search many files for a string. This example finds the string "searchthis." This is useful when I want to find, say, a CSS class name that has changed, and update it in all of my .js, .jsp and .jspf files.

The -niP argument to grep is optional. -n prints line numbers, -i makes search case-insensitive, and -P toggles Perl regular expression syntax.

find \ ( -name "*js" -o -name "*jsp" -o -name "*jspf" \) | xargs grep -niP 'searchthis'
LVL 26

Expert Comment

by:Fadi SODAH (aka madunix)
ID: 29464868
i use the following with my php files, i search first the text take back of files before the replace the old with new ...

cd directory
find . -type f -exec grep -rH <pattern> {} \;
find . -name "*.php" -exec sed -i".bck" -e 's/oldpattern/newpattern/g' {} \;
LVL 16

Expert Comment

ID: 29465128
If you're just searching a directory tree for .html or .php files (for instance) then I have to say give jEdit a try. It's "search in directory" feature is superb.

You can search and replace using regular expressions, which makes finding complex string groups a cinch, and replacing blocks of old code with new code very easy.

Not exactly what you were after, but definitely worth a look if this is about finding text in your text and config files.
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

LVL 68

Expert Comment

ID: 29465403
 use "-print" instead of -print0 and "xargs -n1" instead of "xargs"  alone.
LVL 68

Expert Comment

ID: 29465784
... alternatively, with "-print0" use "xargs -0"

-print0 outputs null-separated strings, xargs needs "-0" to recognize this.

Author Comment

ID: 29469856
Okay, so far this works... almost..... it shows me the line contents, but not the name of the file it was in.

find ./home/mark/domains/markgoodall.com/public_html/front_end_code -name *.php -print | xargs -n1 grep '<head>'

Open in new window

LVL 68

Accepted Solution

woolmilkporc earned 2000 total points
ID: 29471092
find ./home/mark/domains/markgoodall.com/public_html/front_end_code -name *.php -print | xargs -n1 grep -H '<head>'

"-H" will print the filename for each match.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Can you run Linux on a Windows system?  Yep.  Here's how.
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.
Suggested Courses

607 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