• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unix/Linux path

 
Nikolay Tsonkov
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I know this is a trite question but I still can't find the answer. I'm doing a program that has to get the free disk space of a certain folder. The program has to be compatible with java 1.5 so I can't use getFreeSpace() method. So for this purpose I use:

It works fine but when the folder that has to be checked has intervals in the path ( e.g /mnt/test dir/) the command doesn't execute properly and when it tries to get the output from it the program breaks since nothing was output by the end.
I tried to parse the path of the folder by putting '\' before every interval but no luck either. It doesn't work with quotations. What is wrong with that exec() method?
Any suggestions or advises would be appreciated.

Thanks in advanse.
 
Guy Leeds
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The directory path needs to go in single quotes if directories include spaces i.e. for you
df -h '/mnt/test dir/'

So, the next challenge is to set your folderPath to include the single quotes but a bit of experimentation with escape characters should resolve that.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the version of Runtime.exec() that accepts an array of Strings; put one word into each String -- i.e.,

exec(new String[] {"df", "-h", "/mnt/test dir/" });
 
Nikolay Tsonkov
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest,

It worked fine. That was a great tip.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Placing spaces in file/directory names is a horrible thing to do. Even Microsoft has finally wised up. I don't know about Apple.

The reason why it's horrible is because it confuses command-line parsers (and occasionally people, as well). On the original Macintosh, there was no command line to speak of, so they could get away from it, but most of the rest of the world is less GUI-oriented, so as a general rule, if you're given any choice in the matter, don't put spaces in paths.

You can escape spaces in Unix pathnames, using a backslash, but since the backslash is itself magic to Java, you have to double it up, as in "test\\ dir".
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic