LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-28-2010, 10:17 AM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
BASH:find out if volume "foo"/folder "goo" can be written to


My habit is to write BASH scripts that export output to ASCII text files, typically in the same directory as the files they're evaluating. It occurred to me recently that, with read-only volumes, or in directories where my permissions may be "less than 776," that could throw an unfriendly error or two in my direction. To avoid it/work around it, I'd like to know the best way(s) of determining the write-ability of a folder before I run a script on it OR have the script 'ping' for write status/privileges in the directory it's in and dump its output there or elsewhere, as appropriate.

Regarding volumes:
What command would one use, and how would one parse the output?
I have a suspicion that stat might be useful, and I know from perusing the man page that there are %-letter tags that will "pull out" attribute data for a file or folder. Just now I did a 'stat' command, and I noticed in two fields there were zeros. Is this a practical "rule of thumb" when determining whether or not a volume is write-able?

Regarding the permissions of folders:
I'm familiar with the different options to 'ls', but here again I think 'stat' may be the better approach, owing to the attribute "print out" options I mentioned above. Here I suppose it's a question of "which one gives up the goods with less hassle," or "which one can be counted on to 'tell the truth' about folder WXYZ right off the bat." stat I can parse; I have zero practise at pulling ls's teeth, so to speak.

Any advice on this matter would be much appreciated.

BZT
 
Old 08-28-2010, 11:01 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
You can use -w in the test command.
Code:
if [ -w dirname ]; then echo "writable"; fi
http://www.tldp.org/LDP/abs/html/fto.html
 
Old 08-28-2010, 11:03 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,646

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Quote:
Regarding volumes:
What command would one use, and how would one parse the output?
Sorry but to do what?

Quote:
I'd like to know the best way(s) of determining the write-ability of a folder
Whenever I need this information the first place to go is man test. There you will see an answer is -w:
Code:
[[ -w dir ]] && echo yes it is
Remembering that linux see both files and directories as files.
 
  


Reply

Tags
bash, folder, permissions, volume, write


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 11:35 AM
[SOLVED] There is no "menu.lst" in my Ubuntu "/boot/grub/" folder. msbstar Linux - Newbie 18 09-15-2012 02:26 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
BASH get marked RegEx result: "foo s bar" | /foo(.*)bar/ 1 -> " s " hansschmucker Programming 6 10-23-2007 11:34 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM


All times are GMT -5. The time now is 06:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration