BASH Shell Test If a File Is Writable or Not

ADVERTISEMENTS



The -w option

How can you test if a file is writable under UNIX / Linux bash shell scripting and programming language?You can easily test if a file is writable or noting test command under UNIX, Linux, macOS, *BSD family of operating systems when using bash or any other modern shell. This page shows how to test for write access to a file.

The -w option is used to test if a FILE exists and write permission is granted or not. It returns true if a file is writable. The general syntax is as follows:

[ -w filename ] && echo "Writable" || echo "Not Writable"

test -w filename && echo "Writable" || echo "Not Writable"

The test command exits with a status of 0 (true) or 1 (false) depending on the evaluation of EXPR such as [ -w filename ]. Expressions may be unary or binary. Unary eressions are often used to examine the status of a file. There are string operators and numeric comparison operators as well. See below for more information.

Example – Test for write access to a file

Here is a sample shell script (test1.sh):

#!/bin/bash FILE = "$1" [ $# -eq 0 ] && exit 1 if [ -w " $FILE " ] then echo "Write permission is granted on $FILE " else echo "Write permission is NOT granted on $FILE " fi #!/bin/bash FILE="$1" [ $# -eq 0 ] && exit 1 if [ -w "$FILE" ] then echo "Write permission is granted on $FILE" else echo "Write permission is NOT granted on $FILE" fi

Run shell script as follows:

chmod +x test1.sh

Execute the shell script:

bash test1.sh file1

### OR ####

./test1.sh file2



How to check for a file and whether it is readable and writable

The bash shell test command has many more options as follows:

-w FILE : FILE exists and write permission is granted -x FILE : FILE exists and execute (or search) permission is granted -d FILE : FILE exists and is a directory -e FILE : FILE exists -f FILE : FILE exists and is a regular file -r FILE : FILE exists and read permission is granted -s FILE : FILE exists and has a size greater than zero

So one can see if file is readable or not using the -r option. For instance:

[ -r "$filename" ] && echo "File is readable" || echo "Sorry not readable"

Bash shell script example to test writable and readable file

Create a file named test2.sh as follows:

#!/bin/bash FILE = "$1" [ $# -eq 0 ] && exit 1 if [ [ -r " $FILE " && -w " $FILE " ] ] then echo "We can read and write the $FILE " else echo "Access denied" fi #!/bin/bash FILE="$1" [ $# -eq 0 ] && exit 1 if [[ -r "$FILE" && -w "$FILE" ]] then echo "We can read and write the $FILE" else echo "Access denied" fi

Next run it as follows:

./test.sh /etc/passwd

sudo ./test.sh /etc/passwd



Conclusion

You learned how to check for a file and whether it is readable and writable under bash shell. For more information see test command help page or bash command man page here:

man bash

help test

Sample help

test: test [ expr ] Evaluate conditional expression. File operators: -a FILE True if file exists. -b FILE True if file is block special. -c FILE True if file is character special. -d FILE True if file 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 is set-group-id. -h FILE True if file is a symbolic link. -L FILE True if file is a symbolic link. -k FILE True if file has its `sticky' bit set. -p FILE True if file is a named pipe. -r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -S FILE True if file is a socket. -t FD True if FD is opened on a terminal. -u FILE True if the file is set-user-id. -w FILE True if the file is writable by you. -x FILE True if the file is executable by you. -O FILE True if the file is effectively owned by you. -G FILE True if the file is effectively owned by your group. -N FILE True if the file has been modified since it was last read. FILE1 -nt FILE2 True if file1 is newer than file2 ( according to modification date ) . FILE1 -ot FILE2 True if file1 is older than file2. FILE1 -ef FILE2 True if file1 is a hard link to file2. All file operators except -h and -L are acting on the target of a symbolic link, not on the symlink itself, if FILE is a symbolic link. String operators: -z STRING True if string is empty. -n STRING STRING True if string is not empty. STRING1 = STRING2 True if the strings are equal. STRING1 ! = STRING2 True if the strings are not equal. STRING1 < STRING2 True if STRING1 sorts before STRING2 lexicographically. STRING1 > STRING2 True if STRING1 sorts after STRING2 lexicographically. Other operators: -o OPTION True if the shell option OPTION is enabled. -v VAR True if the shell variable VAR is set. -R VAR True if the shell variable VAR is set and is a name reference. ! EXPR True if expr is false. EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne, -lt, -le, -gt, or -ge. Arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than ARG2. See the bash manual page bash ( 1 ) for the handling of parameters ( i.e. missing parameters ) . Exit Status: Returns success if EXPR evaluates to true; fails if EXPR evaluates to false or an invalid argument is given. test: test [expr] Evaluate conditional expression. File operators: -a FILE True if file exists. -b FILE True if file is block special. -c FILE True if file is character special. -d FILE True if file 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 is set-group-id. -h FILE True if file is a symbolic link. -L FILE True if file is a symbolic link. -k FILE True if file has its `sticky' bit set. -p FILE True if file is a named pipe. -r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -S FILE True if file is a socket. -t FD True if FD is opened on a terminal. -u FILE True if the file is set-user-id. -w FILE True if the file is writable by you. -x FILE True if the file is executable by you. -O FILE True if the file is effectively owned by you. -G FILE True if the file is effectively owned by your group. -N FILE True if the file has been modified since it was last read. FILE1 -nt FILE2 True if file1 is newer than file2 (according to modification date). FILE1 -ot FILE2 True if file1 is older than file2. FILE1 -ef FILE2 True if file1 is a hard link to file2. All file operators except -h and -L are acting on the target of a symbolic link, not on the symlink itself, if FILE is a symbolic link. String operators: -z STRING True if string is empty. -n STRING STRING True if string is not empty. STRING1 = STRING2 True if the strings are equal. STRING1 != STRING2 True if the strings are not equal. STRING1 < STRING2 True if STRING1 sorts before STRING2 lexicographically. STRING1 > STRING2 True if STRING1 sorts after STRING2 lexicographically. Other operators: -o OPTION True if the shell option OPTION is enabled. -v VAR True if the shell variable VAR is set. -R VAR True if the shell variable VAR is set and is a name reference. ! EXPR True if expr is false. EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne, -lt, -le, -gt, or -ge. Arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than ARG2. See the bash manual page bash(1) for the handling of parameters (i.e. missing parameters). Exit Status: Returns success if EXPR evaluates to true; fails if EXPR evaluates to false or an invalid argument is given.