Get only the size of a mounted filesystem
Solution 1
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" "
Solution 2
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
Related videos on Youtube
Amoon3234
Updated on September 18, 2022Comments
-
Amoon3234 over 1 year
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? -
Amoon3234 almost 8 yearsYour commands are invalid. Throws error saying that the size column doesn't exist
-
meuh almost 8 yearsmy version is 2.26.2. Try
findmnt --help
to see if it lists which columns it allows. -
Manoj Shekhawat over 3 yearsdf -h /mnt/xyz | awk ' NR==3 ' | awk '{print $1}'