Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Check File Permissions in Bash Script

    How to Check File Permissions in Bash Script

    By RahulJune 3, 20191 Min Read

    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.

    Advertisement

    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.

    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.

    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.

    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

    Related Posts

    tail Command in Linux with Examples

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    View 1 Comment

    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

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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