Testing Condition on File permission Testing Condition on File permission unix unix

Testing Condition on File permission


You can do something like:

Test for execution:

if [ -x "$file" ]; then  #do_somethingfi

Test for writing:

if [ -w "$file" ]; then  #do_somethingfi

Test for reading:

if [ -r "$file" ]; then  #do_somethingfi


stat FILENAME --print=%A

see man stat for more options - you can check the group/owner/etc.


You could use test

The options -r -x -w test for read,execute and write permission.In a shell script the test command is usually entered as [