'How do I remove the header in the df command?

I'm trying to write a bash command that will sort all volumes by the amount of data they have used and tried using

df | awk '{print $1 | "sort -r -k3 -n"}'

Output:

map
devfs
Filesystem
/dev/disk1s5
/dev/disk1s2
/dev/disk1s1

But this also shows the header called Filesystem. How do I remove that?



Solution 1:[1]

Skip the first line, like this:

df | awk 'NR>1 {print $1 | "sort -r -k3 -n"}'

Solution 2:[2]

Count the lines from the output of df with wc and then substract one line to output a headerless df with tail ...

LINES=$(df|wc -l)
LINES=$((${LINES}-1))
df | tail -n ${LINES}

OK - I see oneliner - Here is mine ...

DF_HEADERLESS=$(LINES=$(df|wc -l); LINES=$((${LINES}-1));df | tail -n ${LINES})

And for formated output lets printf loop over it...

printf "%s\t%s\t%s\t%s\t%s\t%s\n" ${DF_HEADERLESS} | awk '{print $1 | "sort -r -k3 -n"}'

Solution 3:[3]

This might help with GNU df and GNU sort:

df -P | awk 'NR>1{$1=$1; print}' | sort -r -k3 -n | awk '{print $1}'

With GNU df and GNU awk:

df -P | awk 'NR>1{array[$3]=$1} END{PROCINFO["sorted_in"]="@ind_num_desc"; for(i in array){print array[i]}}'

Documentation: 8.1.6 Using Predefined Array Scanning Orders with gawk

Solution 4:[4]

Removing something from a command output can be done very simply, using grep -v, so in your case:

df | grep -v "Filesystem" | ...

(You can do your awk at the ...)

When you're not sure about caps, small caps, you might add -i:

df | grep -i -v "FiLeSyStEm" | ...

(The switching caps/small caps are meant as a clarification joke :-) )

Solution 5:[5]

For your specific case, i.e. using awk, @codeforester answer (using awk NR (Number of Records) variable) is the best.

In a more general case, in order to remove the first line of any output, you can use the tail -n +N option in order to output starting with line N:

df | tail -n +2 | other_command

This will remove the first line in df output.

Solution 6:[6]

I normally use one of these options, if I have no reason to use awk:

df | sed 1d

The 1d option to sed says delete the first line, then print everything else.

df | tail -n+2

the -n+2 option to tail say start looking at line 2 and print everything until End-of-Input.

I suspect sed is faster than awk or tail, but I can't prove it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 codeforester
Solution 2
Solution 3
Solution 4 Dominique
Solution 5 Bastien
Solution 6 Scottie H