Matt Fletcher
Matt Fletcher

Reputation: 355

Finding text within files with filenames

I have a number of clients running a piece of software within their public_html directory. The software includes a file named version.txt that contains the version number of their software (the number and nothing else).

I want to write a bash script that will look for a file named version.txt directly within every user's /home/xxx/public_html/ and output both the path to the file, and the contents of the file, i.e:

/home/matt/public_html/version.txt: 3.4.07
/home/john/public_html/version.txt: 3.4.01
/home/sam/public_html/version.txt: 3.4.03

So far all I have tried is:

#!/bin/bash

for file in 'locate "public_html/version.txt"'
do
        echo "$file"
        cat $file
done

But that does not work at all.

Upvotes: 1

Views: 243

Answers (3)

mana
mana

Reputation: 6547

Or do it using find:

find /home -name "*/public_html/version.txt" -exec grep -H ""  {} \;

Upvotes: 1

Zsolt Botykai
Zsolt Botykai

Reputation: 51593

find /home -type f -path '*public_html/version.txt' -exec echo {} " " `cat {}` \;

Might work for you, but you can go without echo and cat ("tricking" grep):

find /home -type f -path '*public_html/version.txt' -exec grep -H "." {} \;

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272217

for i in /home/*/public_html/version.txt; do
   echo $i
   cat $i
done

will find all the relevant files (using shell wildcarding), echo the filename out and cat out the file.

If you want a more concise output, you should investigate grep and replace the echo/cat with an appropriate regular expression e.g.

grep "[0-9]\.[0-9]" $i

Upvotes: 0

Related Questions