Using grep - check the permissions of the file searched

What I need to do is: I need to use the grep command to search for pattern in directory and sub-directories. And also I need to show the permission of file been seached by the grep command.

Could any one please suggest me?
-----------------
$> cat file1.txt
A
-----------------
-----------------------
$> grep -lr '^A$' ../*
../bakup/file1.txt
../test1/file1.txt
-----------------------

In this example, I am searching the pattern "A" in files which is the only value in the line, in the parent directory and sub-directories.

By this I am able to see the files...which contains A as the only value in the line... but what If I need to see the permissions of these files....

Waiting for reply!!

Johny

look into the find command.

man find,,, maybe you can hack some of this script,,,,,, it does not do what you want,, but i think parts of it does-------------------------- look down for the line with "find"

#!/bin/ksh
#
# $0 [-u userID] dir
#
if [[ $setx = "true" ]];then set -x;fi
U_f=false;
for i
do case $1 in
-u*) shift
U_f=true
U=${1};;
-h*) more<<*eof

------------------------------------------------------------------------------------

aha2231
Thu Aug 14 13:59:05 PDT 2008

This script will recursively report out if a file is on tape or if it is on disk
based on ls -s = 256

Usage: $0 [-u $USER ] directory

             -u $USER will return only those files owned by $USER
             directory requested is $1, or default to current directory

  Ex: $0
      $0 .
      $0 dir
      $0 -u $USER dir 

------------------------------------------------------------------------------------

*eof
exit;;

            -*\)     echo "\\007illegal $0 option $1\\007"
                    $0 -h
                    exit;;

            *\)      break;;

    esac
    shift

done

if ${U_f} true; then
U="-user ${U}"
else
U=""
fi

echo USER $USER
echo NODE `hostname`
echo START `date`

dir=${1:-`pwd`}

find ${dir} ${U} -type f -ls | awk ' {

f1="ON %-4s %12.2f %-3s %-7s %-7s %5s %2s %-15s %1s"

flag=$2
s=$7
f=$11
u=$5
month=$8
day=$9
time=$10
group=$6

if(flag=="256" && s/1024 > 256 ) {
q="TAPE"
t[(substr($3,1,1))]++;tsum=tsum+$7
}
else {
q="DISK"
d[(substr($3,1,1))]++;dsum=dsum+$7
}

out1 = sprintf(f1, q, s/1024, "kb", u, group, month, day, time, f)
print out1

}

END{

print tsum/1024/1024/1024 " Gigabytes, and " t["-"] " files are ON TAPE"
print dsum/1024/1024/1024 " Gigabytes, and " d["-"] " files are ON DISK"

}'

exit

Thanks a lot for your reply... :slight_smile:

I am looking into find command and see if i can get out of it some useful stuff...

My motive is to

  1. search a patter in all the files in directory and sub-directories
  2. List all the files which cantains this pattern
  3. see the permission of these files...

I am working on to find the solution, if you can also give some solution with an example, that would be very helpful...

Thanks again!

I progresses till this part... I need to link them...trying to do it now...

first using grep command i get the files which contians this patter.
----------------------
$> grep -lr '^A$' ../*
../bakup/file1.txt
../test1/file1.txt
----------------------

Then find command can be used to see the permission of a file.
----------------------
$> find ../ -name "file1.txt" -exec ls -l {} \;
-rw-rw-r-- 1 johny ABC 2 Sep 25 04:39 ../test1/file1.txt
-rw-rw-r-- 1 johny ABC 2 Sep 25 05:02 ../bakup/file1.txt
----------------------
but the question is how find command will get the file names seached by grep command?

Can anyone suggest?

This is FYI....

I have found the solution.

We would be required to write a script for this.

----------------------------------
#!/bin/ksh

grep -lr '^A$' ../ > finding.txt

cat finding.txt | while read filename
do
ls -l $filename | awk '{print $1, $9}'
done
----------------------------------

----------------------------------
$> ./check.ksh
-rw-rw-r-- ../test1/file1.txt
-rw-rw-r-- ../bakup/file1.txt
----------------------------------

This will show the file found with respective permissions.

You can try this also

grep -lr '^A$' ../ | xargs ls -l | awk '{print$1 " " $9}'

No need for a temporary file, and of course, you want to avoid the Useless Use of Cat.

ls -l `grep -lr '^A$' ../` | awk '{ print $1, $9 }'

If the list of matches is very long, it would be better to use xargs:

grep -lr '^A$' ../ | xargs ls -l | awk '{ print $1, $9 }'

Maybe that's actually more readable as well.

Thanks a lot Bijayant... this is one line answer and working great!!

Wooow... Era.... thanks ...the solution you provided is working fine...

:-).... really a great help!