4

In linux, given a path to a file, is there any way to tell which filesystem it is on?

Specifically, I'm trying to figure out how much space is left on the drive that /home/itsadok/bananas is in. From df I can see how much space is left on the various mounted drives, but it's a hard to figure out which of them is relevant.

By the way, I want to do this from a script, so educated guesses are not allowed.

4
  • please change the question to reflect what you are really after ("How much space is available on the device containing a given file" or something like that).
    – akira
    Oct 28, 2009 at 10:41
  • You're right. Although I would be interested in an answer to the original question.
    – itsadok
    Oct 29, 2009 at 8:29
  • Heh. Just realized this is an answer to the original question, since df tells you which filesystem it is.
    – itsadok
    Oct 29, 2009 at 8:30
  • My vote goes to the original title (if only to find it at some later time).
    – Arjan
    Nov 1, 2009 at 13:37

3 Answers 3

6

Run

df -k /home/itsadok/bananas

(-k for size in kilobytes, you can ignore it if you don't want it)

This will give you the size left for only the filesystem containing the file, at least on AIX.

4
  • works properly on RHEL as well
    – warren
    Oct 28, 2009 at 9:02
  • 4
    also, fwiw, I personally prefer the -h option for human-readable sizes
    – warren
    Oct 28, 2009 at 9:02
  • Also works on Ubuntu and Solaris.
    – innaM
    Oct 28, 2009 at 9:31
  • if you run across installations where df doesn't like a file argument, give it a directory argument instead: df -k $(dirname /home/itsadok/bananas) (or use backticks instead of $( )) Oct 29, 2009 at 11:21
1

I was playing around with df after reading this question and I think this might be of help to someone:
To get it in a script, you have to remove the title line, that is, just keep the output line which has the text /dev/sda1. So I did this:

$ df -h . | grep dev

which gave me this:

/dev/sda1              41G  3.6G   36G  10% /

And to get the 36G part, i did this:

$ df -h . | grep dev | cut -d' ' -f20

Playing around with cut gives the other parts also:

$ df -h . | grep dev | cut -d' ' -f1
/dev/sda1

As itsadok points out below, awk is better for this purpose since a change in number of spaces and/or field widths will break a cut based solution.
So this is better:

$ df -h . | grep dev | awk '{print $4}' 
36G
1
  • 1
    cut seems a little fragile for this purpose, I think the numbers won't with different drive sizes. I would use awk '{print $3}' instead.
    – itsadok
    Nov 1, 2009 at 8:21
0

Since this is in a script:

stat -fc '%a * %S' -- "$fname" |bc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .