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" fiTwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop