Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»How to Check File Permissions in Bash Script

    How to Check File Permissions in Bash Script

    RahulBy RahulApril 7, 20151 Min ReadUpdated:June 3, 2019

    This is good to test a file has enough permission to do read, write or execute operations. For a good programmer, you should use these functions before doing any operations on the file.

    1. Test read permission:

    Below script will check if the given file has read permission for currently logged in user. This will be useful to test before start reading any file inside a shell script.

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/bash
     
    if [ -r /tmp/myfile.txt ]
    then
         echo "File has read permission"
    else
         echo "You don't have read permission"
    fi

    2. Test write permission:

    Below script will check if a given file has to write permission for currently logged in user. This will be useful to test before writing content to any file inside a shell script.

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/bash
     
    if [ -w /tmp/myfile.txt ]
    then
         echo "File has write permission"
    else
         echo "You don't have write permission"
    fi

    3. Test execute permission:

    Below script will check if the given file has execute permission for currently logged in user. This will be useful to test before executing any file inside a shell script.

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/bash
     
    if [ -x /tmp/myfile.txt ]
    then
         echo "File has execute permission"
    else
         echo "You don't have execute permission"
    fi

    bash file if script shell test
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Setup WHM & cPanel on AWS Instances
    Next Article HOw to Get Gravatar Image with PHP

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Bash Select Command (Create Menu in Shell Scripts)

    Updated:December 10, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    1 Comment

    1. mark on August 3, 2019 4:25 am

      Hey Rahul, found this very helpful. Thanks for keeping so brief and straight forward, its appreciated.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.