Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check free space of a directory of linux environment in Java

 
Timmy Gupta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Theodore Casser
Ranch Hand
Posts: 1902
Hibernate Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could see if java.io.File.getFreeSpace() 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds too difficult a question for us beginners. Moving. Probably best on the Linux forum.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Timmy Gupta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did not understand this command. what is the meaning of each character ?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11887
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic