aspose file tools*
The moose likes Linux / UNIX and the fly likes Unix/Linux path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Unix/Linux path" Watch "Unix/Linux path" New topic

Unix/Linux path

Nikolay Tsonkov

Joined: May 31, 2008
Posts: 19
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

Joined: Nov 06, 2003
Posts: 18
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

Joined: Jul 08, 2003
Posts: 24187

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/" });

[Jess in Action][AskingGoodQuestions]
Nikolay Tsonkov

Joined: May 31, 2008
Posts: 19
Thank you Ernest,

It worked fine. That was a great tip.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16141

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".

Customer surveys are for companies who didn't pay proper attention to begin with.
I agree. Here's the link:
subject: Unix/Linux path