Bash – Get only the size of a mounted filesystem

bashcutdisk-usagegrepshell-script

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?

Best Answer

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" "
Related Question