Check for file permission


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for file permission
# 1  
Old 02-25-2014
Check for file permission

Hi,

I have an array that has several directory paths and file paths.

I use a for loop to traverse through the array and check for files and not directories using the if condition.

I wish to display all files [not directories] that have permissions lesser than what the user has specified.

For example:

Code:
-rwxrwx---   2 user1      sie   8273920 Feb 24 15:13 /opt/app/hello.txt
-rwxr-x---   2 user1      sie   8273920 Feb 24 15:13 /opt/app/yellow.txt
-rwxrwxr-x   2 user1      sie   8273920 Feb 24 15:13 /opt/app/fellow.txt

So, if the user gives 770 which is read in a variable $1 then.

Expected result should be:

Code:
-rwxrwx---   2 user1      sie   8273920 Feb 24 15:13 /opt/app/hello.txt
-rwxrwxr-x   2 user1      sie   8273920 Feb 24 15:13 /opt/app/fellow.txt

Because the second entry is with permission 750 which is lesser permission the 770.
# 2  
Old 02-25-2014
find with perm will do.
Below code will print all the files with permissions >= 750

Code:
find . -type f -perm -750

Below is the test case
Code:
-bash-4.1$ ls -lrt
total 12
-rwxr-x--- 1 username groupnm  55 Feb 25 05:43 table.dat
-rw-r--r-- 1 username groupnm 109 Feb 25 05:43 sample.dat
-rwxrwx--- 1 username groupnm 113 Feb 25 05:43 manipu.dat
-bash-4.1$ find . -type f -perm -750 | xargs ls -lrt
-rwxr-x--- 1 username groupnm  55 Feb 25 05:43 ./table.dat
-rwxrwx--- 1 username groupnm 113 Feb 25 05:43 ./manipu.dat
-bash-4.1$ find . -type f -perm -770 | xargs ls -lrt
-rwxrwx--- 1 username groupnm 113 Feb 25 05:43 ./manipu.dat
-bash-4.1$ find . -type f -perm -600 | xargs ls -lrt
-rwxr-x--- 1 username groupnm  55 Feb 25 05:43 ./table.dat
-rw-r--r-- 1 username groupnm 109 Feb 25 05:43 ./sample.dat
-rwxrwx--- 1 username groupnm 113 Feb 25 05:43 ./manipu.dat
-bash-4.1$

# 3  
Old 02-25-2014
What have you tried so far?
# 4  
Old 02-25-2014
Quote:
Originally Posted by chacko193
What have you tried so far?
I have the exact file name in an Array, so how could i use find ?

Code:
echo $arr[3]

-rwxrwx--- 2 user1 sie 8273920 Feb 24 15:13 /opt/app/hello.txt
Please let me know how can i use find with the array, if i can't, then what is the alternative ?
# 5  
Old 02-25-2014
If the array has 10 elements, i.e., $arr[0] to $arr[9], below is the code

Code:
for i in $(seq 0 9);
do
find $(dirname $(echo ${arr[${i}]} | awk '{print $9}')) -type f -name $(basename $(echo ${arr[${i}]} | awk '{print $9}')) -perm -750
done

This User Gave Thanks to SriniShoo For This Post:
# 6  
Old 02-25-2014
As per the requirement that you specified in your first post you can try this:
Code:
find /path/to/your/directory -type f -perm -$yourPerm -exec ls -l {} \;

# 7  
Old 02-25-2014
Quote:
Originally Posted by mohtashims
I have an array that has several directory paths and file paths.
I use a for loop to traverse through the array
You shouldn't do that. for-loops can break if you have too many elements. Use a while-loop instead:

Code:
(( counter = 0 ))
while [ $counter -lt ${#array[*]} ] ; do
     command "${array[$counter]}"
     (( counter += 1 ))
done

Quote:
I wish to display all files [not directories] that have permissions lesser than what the user has specified.
What do you mean by "lesser"? If i specify "660" and the file turns out to be "750", is that lesser, more or something entirely different? How about "755" or "555"?

Please clarify.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

File permission

Hi, Could you please tell me the OS command to grant as similar to below permission? rwxrwxr-x OS -- HP-UX Regards, Maddy (8 Replies)
Discussion started by: Maddy123
8 Replies

2. Shell Programming and Scripting

perl script to check read/write/execute permission for 'others'

I want to check access rights permissions not for 'user', not for 'group', but for 'others'. I want to do it by system command in which i want to use 'ls -l' and 'awk' command. I have written the following program : #!/usr/bin/local/perl #include <stdlib.h> system ("ls -l | awk... (1 Reply)
Discussion started by: shubhamsachdeva
1 Replies

3. Shell Programming and Scripting

shell script to check permission before copy over

Hi, I am writing some shell script to check the file owner permission whether the write bit is turn on. If it is turn on it will copy the file to the destination else it will just prompt user to change the file and skipping it. However, I am starting getting loss here as I have couple of... (2 Replies)
Discussion started by: wanaka
2 Replies

4. Shell Programming and Scripting

File permission check in SFTP mode

Hi, I have requirement to get a file from a remote location using SFTP only if the file has read/write permission either to the user or to the group. Currently i m taking the ls of the required file and storing it in a flat file to check the file permission. Please advice is there any... (3 Replies)
Discussion started by: ravin
3 Replies

5. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

6. Shell Programming and Scripting

Command to check the user's permission in UNIX ??

Hi, What is the command to check the user's permission in UNIX ? e.g: user in UNIX is "myuser" The result should be number format. e.g: 755, 644, etc... Thanks.. :) (3 Replies)
Discussion started by: suigion
3 Replies

7. Cybersecurity

file permission/acl: 2 users with write access on 1 file...

Hello, i need some help/advice on how to solve a particular problem. these are the users: |name | group | ---------- --------------- |boss | department1 | |assistant | department1 | |employee | department1 | |spy | department2 | this is the... (0 Replies)
Discussion started by: elzalem
0 Replies

8. Shell Programming and Scripting

sftp - permission setting check

Hello, I am doing a transfer of a file from one HP-UX ( umask : 033 ) server to another HP-UX server ( umask : 033 ) and file got transferred sucessfully. However I could see the file permission gets changed at the receiving end. Would like to know the reason for that? Details below: ... (3 Replies)
Discussion started by: jansat
3 Replies

9. UNIX for Dummies Questions & Answers

File Permission

Hi, When I listed one directory in Sun, it showed that : -rwsr-xr-x 1 root bsmbin 78004 Oct 21 2004 bsmprsm I don't know meaning of the character "s" in "rws" above. I have searched in Sun admin documents but no result. Would you please explain it ? :) Thank you so much. (1 Reply)
Discussion started by: msg098
1 Replies

10. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies
Login or Register to Ask a Question