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

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

#!/bin/bash
# Shell script to find out whether file has read, write and execute 
# permission
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter file name : "
read file
 
# find out if file has write permission or not
[ -w $file ] && W="Write = yes" || W="Write = No"
 
# find out if file has excute permission or not
[ -x $file ] && X="Execute = yes" || X="Execute = No"
 
# find out if file has read permission or not
[ -r $file ] && R="Read = yes" || R="Read = No"
 
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • Ayushi Aug 8, 2017 @ 9:37

    How to write a shell script to display the specified number of lines from the given file. Accept a file name and check whether the file name entered exists and has both read and write permission for the owner and the number of lines should be > 0 and < 20. ???

  • yagnesh kevadiya Apr 17, 2017 @ 7:27
    echo "Executable files in the current directory...."
    for File in *
        do if [ -x $File ]
                then
                        echo $File
                fi
        done
    
    echo "Directories in the current directory...."
    for dir in *
       do if [ -d $dir ]
               then
                       echo $dir
               fi
        done
    
    echo "Zero sized files in the current directory...."
    
    for file in `find -type f -size 0c`
    	do 
    			echo $file
    		
    	done
    
  • Remo Ali Ahamed Dec 28, 2016 @ 11:18
    // Coded by the Remo
    echo -n "Enter the file or directory name    :"
    read name
    if test -f $name 
          then 
                  echo "Its is a file and it has "
                  if test -r $name 
                         then 
                               echo "Read permission "
                          fi
                  if test -w $name 
                         then 
                               echo "Write permission "
                          fi
                   if test -x $name 
                         then 
                               echo "Execute permission "
                          fi
    elif test -d $name 
          then 
                  echo "Its is a directory and it has "
                  if test -r $name 
                         then 
                               echo "Read permission "
                          fi
                  if test -w $name 
                         then 
                               echo "Write permission "
                          fi
                   if test -x $name 
                         then 
                               echo "Execute permission "
                          fi
    else 
            echo "Does not find any file or directory named $name
    fi
    
    
    
    
    
    
    
    
    
    
    
  • Remo Ali Ahamed Dec 28, 2016 @ 11:15
    // Coded by the Remo
    echo -n "Enter the file or directory name    :"
    read name
    if test -f $name 
          then 
                  echo "Its is a file and it has "
                  if test -r $name 
                         then 
                               echo "Read permission "
                          fi
                  if test -w $name 
                         then 
                               echo "Write permission "
                          fi
                   if test -x $name 
                         then 
                               echo "Execute permission "
                          fi
    elif test -d $name 
          then 
                  echo "Its is a directory and it has "
                  if test -r $name 
                         then 
                               echo "Read permission "
                          fi
                  if test -w $name 
                         then 
                               echo "Write permission "
                          fi
                   if test -x $name 
                         then 
                               echo "Execute permission "
                          fi
    else 
            echo "Does not find any file or directory named $name
    fi
  • chandu Apr 24, 2014 @ 11:47

    how to know the files and file permissions from directory

  • cykik Dec 4, 2011 @ 14:30

    How can I compare permission of two different files?

  • Dhiraj Nov 10, 2010 @ 21:41

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

    • Anonymous Oct 30, 2011 @ 2:00

      #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

    • Remo Ali Ahamed Jan 2, 2017 @ 15:02
      echo enter the filename
      read file
      gr=` ls -li $file | cut -c13`
      gw=` ls -li $file | cut -c14`
      gx=` ls -li $file | cut -c15`
      
      
      if test $gr = r
      then
              echo $file has read permissoin
      fi
      if test $gw = w
      then
              echo $file has write  permissoin
      fi
      if test $gx = x
      then
              echo $file has execute permissoin
      fi
      
      
  • Anonymous Aug 13, 2010 @ 6:48

    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

    ~
    ~
    ~

  • Vijay May 26, 2008 @ 10:24

    Good work

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.