11

I am trying to collect information on the amount of space left on different servers. So when I execute df -k i get output as:

Filesystem  1024-blocks    Used     Avail Capacity  Mounted on
/dev/ad1s1f   125925198 2568970 113282214     2%    /builds

And sometimes the output comes as :

Filesystem           1K-blocks      Used Available Use% Mounted on
10.102.1.123:/storage/disk1/build
                     10735331328 10597534720 137796608  99% /buildbackup

Now I want to fetch data on the disk space available. So let me know how to fetch data from a particular column.

Ram Krishna
  • 191
  • 1
  • 1
  • 13

2 Answers2

25

You can for example say:

df --output=source,avail

Or as commented by Tim Bunce, you can use --direct to prevent the long filesystem name make the line split in two. This will show the filesystem as -.

From man df:

--output[=FIELD_LIST]

use the output format defined by FIELD_LIST, or print all fields if FIELD_LIST is omitted.

...

FIELD_LIST is a comma-separated list of columns to be included. Valid field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent', 'size', 'used', 'avail', 'pcent' and 'target' (see info page).

--direct

show statistics for a file instead of mount point

Test

$ df --output=source,avail
Filesystem               Avail
/dev/sda7            321675536
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • When i execute the above command on my system, following message is displayed. `# df -k --output=source,Avail df: illegal option -- - usage: df [-b | -g | -H | -h | -k | -m | -P] [-aciln] [-t type] [file | filesystem ...]` – Ram Krishna Mar 02 '15 at 14:29
  • Note you need to say `avail` (lowercase). If this was a type and the problem persists: what system are you working on? – fedorqui Mar 02 '15 at 14:32
  • The machine I am using is FreeBSD6.2 – Ram Krishna Mar 02 '15 at 14:38
  • Did you try with `avail` instead of `Avail`? – fedorqui Mar 02 '15 at 14:39
  • Yes I tried that and the output is same as before. `# df --output=source,avail df: illegal option -- - usage: df [-b | -g | -H | -h | -k | -m | -P] [-aciln] [-t type] [file | filesystem ...] ` – Ram Krishna Mar 02 '15 at 14:40
  • It's a pity, it works in my GNU du (`df --version`). Give a try with `--direct` to get a shorter output. – fedorqui Mar 02 '15 at 14:42
  • `df --` option is illegal on this system (FreeBSD 6.2) I think. Is there any other way to select a column based on column name? – Ram Krishna Mar 02 '15 at 14:44
  • @RamKrishna you should check `man df`, that's how I found the `--output` option. – fedorqui Mar 02 '15 at 14:49
  • There is no option that starts with `--` option when I do `man df` on my system – Ram Krishna Mar 02 '15 at 14:51
  • @RamKrishna ok. Then, try to find other options matching your requirements. Otherwise, you'll have to parse its output. – fedorqui Mar 02 '15 at 14:53
  • Actually currently i am parsing the output to get the required data. But 2 test cases are failing in that case also, since i am doing `ssh` to different versions of **FreeBSD6.2** and **Windows** system the output of `df` command is varying. The best way to get the required result looks to be obtained by fetching the data using column name. But since it looks like not possible on the FreeBSD i am using. I will have to add up code to parse and get the required output. Thanks for your help – Ram Krishna Mar 02 '15 at 14:57
  • @RamKrishna From `man df` in Free BSD (http://www.freebsd.org/cgi/man.cgi?df%281%29) I see you can use `df -m`, for example, to get the output in MB. This way the numbers will be smaller and you might not get a multiline output. – fedorqui Mar 02 '15 at 15:00
6

fedorqui's solution is cleaner, but only works for df

A more general approach is to collapse multiple spaces to a single space using sed 's/ \+/ /g' or tr -s ' ', then use cut with spaces as delimiters: cut -d" " -f 4 So the command is:

df -k | tr -s ' ' | cut -d" " -f 4

Which results in something like this:

Available
26027952
854220
68376208

The same approach will work with other commands that output data to columns. For instance ls -l | tr -s ' ' | cut -d" " -f 6,7,8 will print the time columns from ls -l:

Dec 30 17:46
Mar 1 15:33
Mar 1 14:58
Mar 2 00:00
Jan 5 14:20
Mar 1 15:33
Feb 26 11:57
Feb 4 11:11
Mar 1 14:57
Malt
  • 28,965
  • 9
  • 65
  • 105
  • 1
    well it only works in `df` because the question is regarding `df` :) Also, note you can squeeze spaces using `tr -s ' '` – fedorqui Mar 02 '15 at 12:19
  • 1
    @fedorqui I know the question was about df, and as I said, your answer is better. However, I still felt like giving a more general answer in case someone needs it. – Malt Mar 02 '15 at 12:22
  • Good point! Note, though, that the problem with `df` is that sometimes gets such a big filesystem that has to print it in two different lines, and this would be difficult to handle with `sed`. – fedorqui Mar 02 '15 at 12:23
  • When i execute the above command: `df -k | sed 's/ \+/ /g' | cut -d" " -f 4` The output is blank. `# df -k . | sed 's/ \+/ /g' | cut -d" " -f4 # df -k . | sed 's/ \+/ /g' ` – Ram Krishna Mar 02 '15 at 14:33