≡ Menu

BASH Shell: Test If a File Is Writable or Not

Q. How can you test if a file is writable under UNIX / Linux bash shell scripting?

A. You can easily test if a file is writable or noting test command under UNIX / Linux.

The -w option

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.

General syntax is as follows:
[ -w filename ] && echo "Writable" || echo "Not Writable"
test -w filename && echo "Writable" || echo "Not Writable"

Here is a sample shell script:

[ $# -eq 0 ] && exit 1
if [ -w "$FILE" ]
   echo "Write permission is granted on $FILE"
   echo "Write permission is NOT granted on $FILE"
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 0 comments… add one }

Leave a Comment