Shell Script to find out whether file has read, write and execute permission

by on April 5, 2008 · 6 comments

There are three specific permissions on Unix-like (including Linux / Mac OS x) systems that apply to each user or class:
=> The read permission (r), which grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory
=> The write permission (w), which grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
=> The execute permission (x), which grants the ability to execute a file. This permission must be set for executable binaries in order to allow the operating system to run them. When set for a directory, this permission grants the ability to traverse its tree in order to access files or subdirectories, but not see files inside the directory (unless read is set).

SHELL CONDITIONAL EXPRESSIONS

You can use conditional expressions to find out file permissions. These are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.

Conditional ExpressionMeaning
-a file True if file exists.
-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-g file True if file exists and is set-group-id.
-h file True if file exists and is a symbolic link.
-k file True if file exists and its "sticky" bit is set.
-p file True if file exists and is a named pipe (FIFO).
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than zero.
-t fd True if file descriptor fd is open and refers to a terminal.
-u file True if file exists and its set-user-id bit is set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-O file True if file exists and is owned by the effective user id.
-G file True if file exists and is owned by the effective group id.
-L file True if file exists and is a symbolic link.
-S file True if file exists and is a socket.
-N file True if file exists and has been modified since it was last read.

Sample Shell Script

  1. #!/bin/bash
  2. # Shell script to find out whether file has read, write and execute
  3. # permission
  4. # -------------------------------------------------------------------------
  5. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # -------------------------------------------------------------------------
  11.  
  12. echo -n "Enter file name : "
  13. read file
  14.  
  15. # find out if file has write permission or not
  16. [ -w $file ] && W="Write = yes" || W="Write = No"
  17.  
  18. # find out if file has excute permission or not
  19. [ -x $file ] && X="Execute = yes" || X="Execute = No"
  20.  
  21. # find out if file has read permission or not
  22. [ -r $file ] && R="Read = yes" || R="Read = No"
  23.  
  24. echo "$file permissions"
  25. echo "$W"
  26. echo "$R"
  27. echo "$X"


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Pingback: Check to file permissions « personal storage for public use :)

  • Vijay

    Good work

  • Anonymous

    clear
    echo “Enter the filename:”
    read f1
    if [ -f $f1 ]
    then
    if [ -r $f1 ]
    then
    if [ -w $f1 ]

    echo “File is reable and writable”
    else
    echo “reable”
    fi
    else
    echo “writable”
    fi
    else
    echo “the entered name is not a file”
    fi

    ~
    ~
    ~

  • Dhiraj

    I need code which will give me permission for group or other on given file. does anybody has it ???

  • Anonymous

    #in my current dir files are imagin
    # there are 3 files …dhiraj (dhiraj _ r w x r w x r w x),yasin(_ r _ _ _ _ _ _ _),
    #shell1(_r _ _ r_ x _ _ _)

    #it use for all person like group,other and user
    for $filename in `ls`
    do
    if [ -w $filename -a -r $filename -a -x $filename ]
    then
    echo $filename
    fi
    done
    #output
    dhiraj

  • cykik

    How can I compare permission of two different files?

Previous Script:

Next Script: