17

I want to get only let's say third line of df -h output.

What kind of command should I use?

2
  • 16
    Note for the future: this question is very very basic. It is typically encouraged to show certain effort and what you've tried before asking. Jun 14, 2016 at 18:30
  • While it may be legit to get the 3rd line of a file with a presumed static format, that is different than the 3rd line of a diagnostic program with output that various depending on the state of the system. For instance mounting a new drive/partition alphabetically before your current 3rd line blows it. Thus @Random832's answer below is the best.
    – MarkHu
    Jun 15, 2016 at 22:36

7 Answers 7

32

This will output the 3rd line, regardless of content.

df -h | sed -n 3p
1
  • +1 for smallest answer: df -h|sed -n 3p.
    – EKons
    Jun 16, 2016 at 10:31
16

The df command actually accepts an argument identifying the filesystem you want. So you could use, for example, df /home or df /dev/sda3.

If you intend to parse the output for a script, you'll want to use df -P to guarantee it never wraps to multiple lines. So, for example, you could use df -Ph /home | tail -n +2 (but if you're parsing output for a script, be aware of the possibility of filenames with spaces in them)

3
  • 2
    That's it! Instead of parsing the output, use the command to provide just what you want.
    – fedorqui
    Jun 15, 2016 at 11:11
  • 1
    But the OP said he wanted line 3. df /home provides a header. You're interpreting the user's purpose. You may be right. You may be wrong, who knows?
    – Mike S
    Jun 15, 2016 at 17:58
  • @MikeS A dozen other answers already covered getting line 3 with no context. I provided additional information so that the OP will have it if it is useful for him. And tail -n +2 removes the header.
    – Random832
    Jun 16, 2016 at 15:35
14

You can use a combination of head and tail:

df -h | head -3 | tail -1

Or

df -h | tail -n +3 | head -1

But note that, df allows to filter the output from the options of df itself, you should look at those first before using any external command.

Check man df.

2
  • 4
    +1 for mention that df can filter specific entries Jun 14, 2016 at 13:48
  • 2
    Indeed, it's almost always better to filter the data itself before piping to external programs. Jun 15, 2016 at 3:00
10

Awk is text processing tool, so it's appropriate for this task

df -h | awk 'NR == 3'
6

I assume you're not necessarily looking for the third line, but for the line that either mentions a specific disk (e.g. /dev/sda3) or a specific mount point (e.g. /home).

So instead of just displaying the third line, which is insecure because the output order might change and you might get a different result, you can filter the output by content and show only the lines that match a specific pattern or contain a keyword.

Your tool for this is grep.

For example if you want the line about device /dev/sda3, you type:

df -h | grep '/dev/sda3'

If you want the line mentioning your home directory as mount point, use:

df -h | grep '/home'
1
  • 3
    Or df -h /dev/sda3 or df -h /home (the latter works even if it's not a mount point).
    – Random832
    Jun 14, 2016 at 16:01
6

You could also use perl:

df -h | perl -ne 'print if $.==3' 

The $. is the current line number, so you could print the Nth line with perl -ne 'print if $.==N'.

2
  • 1
    This is why Perl is still relevant in my view. It combines sed and awk with a structured programming language that (while many will say is really really ugly [and I might agree]) is at least consistent and very useful for these short one-liners. Thanks terdon. This is almost as succinct as the sed example, if a bit more verbose.
    – Mike S
    Jun 15, 2016 at 18:01
  • @MikeS glad to help. As a Perl hacker, however, I must point out that Perl code is only as ugly as you make it. Good programmers can write beautiful code in Perl, it's just that the langauge doesn't force you to and it is easy not to.
    – terdon
    Jun 15, 2016 at 18:05
2

My first idea would be a head-tail-construct.

Example:

df -h | head -3 | tail -1

head -3 causes the output to stop after three lines and tail -1 will output only the last line.

Alternatively, if you know, how the output will look like, you could also use grep to find lines containing a certain string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .