Using grep - check the permissions of the file searched


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep - check the permissions of the file searched
# 1  
Old 09-26-2008
Question 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

Last edited by Johny001; 09-26-2008 at 02:34 AM.. Reason: Just to make it more clear to understand!
# 2  
Old 09-26-2008
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
# 3  
Old 09-26-2008
Thanks a lot for your reply... :-)

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!
# 4  
Old 09-26-2008
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?
# 5  
Old 09-26-2008
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.
# 6  
Old 09-26-2008
You can try this also

grep -lr '^A$' ../ | xargs ls -l | awk '{print$1 " " $9}'
# 7  
Old 09-26-2008
No need for a temporary file, and of course, you want to avoid the Useless Use of Cat.

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

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

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

Maybe that's actually more readable as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What's the best way to check file permissions before moving files if needed?

Hello, I would like to know if it's a good practice to check the file permissions of the contents of a directory before moving them. For example: mv -- "$directory"/* "$directory"/.* "$directory"/..?* "$destination"The variables $directory and $destination contain the path to an existing... (6 Replies)
Discussion started by: Cacializ
6 Replies

2. Shell Programming and Scripting

Cut line from searched file if grep find neighbor columns

Hello All, While searching for the question, I found some answers but my implementation is not giving expected output. I have two files; one is sourcefile, other is named template. What I want to do is to search each line in template, when found all columns, cut the matching line from source... (4 Replies)
Discussion started by: baris35
4 Replies

3. UNIX for Dummies Questions & Answers

Is there a way to check when the permissions for the file got changed in AIX

Is there a way to check when the permissions for the file got changed in AIX IS there some file which logs all these details? Best regards, Vishal (4 Replies)
Discussion started by: Vishal_dba
4 Replies

4. Shell Programming and Scripting

To check the file permissions using python scripting

Hi, For a particular set of files, am trying to check if they are writable. i.e., checking whether they are having permissions greater than 755. Am able to check this using the statement: "if (os.path.isfile(FILE_PATH) and (os.stat(FILE_PATH).st_mode & 0777) == 0777):" But the problem... (1 Reply)
Discussion started by: arjun_arippa
1 Replies

5. Programming

To check the file permissions using python scripting

Hi, For a particular set of files, am trying to check if they are writable. i.e., checking whether they are having permissions greater than 755. Am able to check this using the statement: "if (os.path.isfile(FILE_PATH) and (os.stat(FILE_PATH).st_mode & 0777) == 0777):" But the problem here... (0 Replies)
Discussion started by: arjun_arippa
0 Replies

6. Shell Programming and Scripting

Inserting file content into a searched pattern

Hi, i have to insert the content of source.txt into the searched pattern of the file second.txt. $cat source.txt One Two Three . . $cat second.txt This is second file pattern match start here pattern match end here end of the file so the result will be like this (4 Replies)
Discussion started by: posix
4 Replies

7. Shell Programming and Scripting

How to check file permissions from a script.

hello, I have to write a script to run the other script inside it.So iam planning to write like this? first check the perimissions of the file. Alogorthim ---------- if(!filepermissions == execute) then echo" Permissions denined" else execute the script. file name is : load_mf.sh... (1 Reply)
Discussion started by: rajkumar_g
1 Replies

8. UNIX for Dummies Questions & Answers

Grep or other ways to output line above and/or below searched line

Hi all, Would like to know how I could search for a string 'xyz' but have the output show the line plus the line above and/or below all lines found. eg. search for xyz from file containing: abc 12345 asdf xyz asdfds wwwww kjkjkj ppppp kkkxyz eeee zzzzz and the output to... (2 Replies)
Discussion started by: sammac
2 Replies

9. Shell Programming and Scripting

How to use grep to check NAN value and nonexistense element in file

Hi, I have a file which computes fields number and some column produced "nan" I would like to convert the "nan" value to 0 basically. q=`echo $i $j |awk '{printf("%f",($2/($1+$2)))}' The above is the command which I use for computation. However, I would like to check if $1 and $2 is 0, it... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Shell Programming and Scripting

Please Help. Strings in file 1 need to be searched and removed from file 2

Please help. Here is my problem. I have 9000 lines in file a and 500,000 lines in file b. For each line in file a I need to search file b and remove that line. I am currently using the grep -v command and loading the output into a new file. However, because of the size of file b this takes an... (4 Replies)
Discussion started by: mjs3221
4 Replies
Login or Register to Ask a Question