Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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 '09 at 10:41
    
You're right. Although I would be interested in an answer to the original question. –  itsadok Oct 29 '09 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 '09 at 8:30
    
My vote goes to the original title (if only to find it at some later time). –  Arjan Nov 1 '09 at 13:37
add comment

3 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
works properly on RHEL as well –  warren Oct 28 '09 at 9:02
4  
also, fwiw, I personally prefer the -h option for human-readable sizes –  warren Oct 28 '09 at 9:02
    
Also works on Ubuntu and Solaris. –  innaM Oct 28 '09 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 $( )) –  quack quixote Oct 29 '09 at 11:21
add comment

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
share|improve this answer
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 '09 at 8:21
add comment

Since this is in a script:

stat -fc '%a * %S' -- "$fname" |bc
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.