4

I want to get only the total size of a mounted filesystem. But the catch is that I only know about the mount point. So, I thought of using the df command.

To get the size of this mounted filesystem, I ran the following command:

df --output=target,size | grep -w /mnt/xyz

The result that I got was something like this:

/mnt/xyz             4339044

I know how to use cut but it was of no use here as the space between the string and the integers is unknown to me. Is there a way to just print this size on the terminal?

6

You can do it without the grep:

df --output=target,size /mnt/xyz | awk ' NR==2 { print $2 } '

df accepts as argument the mount point; you can tell to awk to print both the second line only (NR==2) , and the 2nd argument, $2.

Or better yet, cut the target as you are not outputting it, and it becomes:

df --output=size /mnt/xyz | awk ' NR==2 '

When I was a begginer, I also did manage to get around cut limitations using tr -s " " (squeeze) to cut redundant spaces as in:

df --output=target,size /mnt/xyz | tail -1 | tr -s " " | cut -f2 -d" "
4

There is also the findmnt command, which can print the number of bytes or a "human" number (powers of 1024 with non-iso abbreviations, sadly):

$ findmnt -no size /mnt/xyz
 9.7G
$ findmnt -bno size /mnt/xyz
10434699264
  • Your commands are invalid. Throws error saying that the size column doesn't exist – Abhimanyu Saharan Jun 24 '16 at 20:09
  • 1
    my version is 2.26.2. Try findmnt --help to see if it lists which columns it allows. – meuh Jun 24 '16 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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