File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes Check free space of a directory of linux environment in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Check free space of a directory of linux environment in Java" Watch "Check free space of a directory of linux environment in Java" New topic

Check free space of a directory of linux environment in Java

Timmy Gupta

Joined: Dec 06, 2008
Posts: 6
I need to run a shell script in linux from Java; but before running the script there should be minimum of 2GB space available in linux directory. Could you please tell me a better approach for this in Java?

Think before you speak as you might hurt or break someone's heart.
Theodore Casser
Ranch Hand

Joined: Mar 14, 2001
Posts: 1902

You could see if gives you the answer you need (it's new in Java SE 6 - you might also check File.getUsableSpace() as an alternative).

Check the Javadocs if you have questions on its usage, as well as caveats to its accuracy.

Theodore Jonathan Casser
SCJP/SCSNI/SCBCD/SCWCD/SCDJWS/SCMAD/SCEA/MCTS/MCPD... and so many more letters than you can shake a stick at!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
Sounds too difficult a question for us beginners. Moving. Probably best on the Linux forum.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Just for giggles, here's a shell script version, in case that works better:

DIRFREE=`df -P /var/lib/mywork | tail -n 1 | tr -s ' ' ':' | cut --delimiter=':' --fields=4`

You can then check $DIRFREE. Note that the "quotes" are back-tick characters (usually on the keyboard left of the "1" key) and not apostrophe/single-quotes. And that the "cut" options have double dashes, not single ones. And finally, that there's exactly one space between the single quotes on the "tr" command.

This sets the number of free bytes on the drive containing /var/lib/mywork into the DIRFREE variable.

An IDE is no substitute for an Intelligent Developer.
Timmy Gupta

Joined: Dec 06, 2008
Posts: 6
Thanks a lot for the command, it worked.

I tried to understand the command and came to know that this will also work
df -P /var/lib/mywork | tail -n 1 | tr -s ' ' ':' | cut -d : -f 4

And to have the output in human readable form, the command could be
df -P -h /var/lib/mywork | tail -n 1 | tr -s ' ' ':' | cut -d : -f 4
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 850
i did not understand this command. what is the meaning of each character ?

SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog-
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

Start by looking at each command separately, and look at the man pages for each command. Preferably you should try running each command piece by piece on your own computer so you can see how it works.

The output of each command is piped to the next command using the pipe symbol (|), so you can easily break it up piece by piece:

  • df -P /var/lib/mywork
  • df -P /var/lib/mywork | tail -n 1
  • ...

  • By looking at the man page for df, it is easy to see that the -P option specifies that the output of df should be in POSIX format - a format that should be the same on all *nix systems, whether you are on a SysV, AIX, HPUX, Linux, BSD, MacOS, FTX, ... system.

    As you look at the man pages, you can then ask individual questions about different commands and/or the parameters they take if you are still confused.

    Doing it this way will help you understand what is going on in far more detail, and you will then be able to customize it and/or build your own scripts to meet your own needs.

    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    I agree. Here's the link:
    subject: Check free space of a directory of linux environment in Java
    It's not a secret anymore!