BASH Shell: Test If a File Is Writable or Not

by on March 12, 2008 · 0 comments· last updated at December 12, 2008

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:

#!/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


You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , , , , ,

Previous Faq:

Next Faq: