5

What would be the correct CL sequence to execute a df -h and only print out the mount name and used space (percentage)? I'm trying to do a scripted report for our servers.

I tried

df -h | awk '{print $1 $4}'

which spits out

$df -h | awk '{print $1 $4}'
FilesystemAvail
/dev/sda164G
udev3.9G
tmpfs1.6G
none5.0M
none3.9G
none100M
/home/richard/.Private64G

How would you change this to add spacing? Am I selecting the right columns?

Rick
  • 591
  • 1
  • 9
  • 23

3 Answers3

9

Try this:

df -h | awk '{if ($1 != "Filesystem") print $1 " " $5}'

Or just

df -h | awk '{print $1 " " $5}'

if you want to keep the headers.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
5

You are almost there:

df -h | awk 'NR>1{print $1, $5}'
perreal
  • 94,503
  • 21
  • 155
  • 181
1

The issues with your code are what input to process, and how to format the output.

As an example, this awk selects records that have a % symbol at the end of field five, and put a space between the two output fields.

 df -h | awk '$5 ~ /\%$/ {print $1 " " $5 }'

Everything else is just refining those two things.

janm
  • 17,976
  • 1
  • 43
  • 61