Skip to content

Instantly share code, notes, and snippets.

@dvu4
Last active January 19, 2023 23:00
Show Gist options
  • Save dvu4/9339acb6f1fe0e764d802c15f431ab2c to your computer and use it in GitHub Desktop.
Save dvu4/9339acb6f1fe0e764d802c15f431ab2c to your computer and use it in GitHub Desktop.
convert a table from bash command into json with jq

To check Disk Space On Linux and Unix operating systems, you can use the df command to get a detailed report on the system’s disk space usage.

df -h

The output

Filesystem       Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk3s1s1  926Gi   20Gi  803Gi     3%  502068 4293294021    0%   /
devfs           205Ki  205Ki    0Bi   100%     710          0  100%   /dev
/dev/disk3s6    926Gi  7.0Gi  803Gi     1%       7 8418661400    0%   /System/Volumes/VM
/dev/disk3s2    926Gi  857Mi  803Gi     1%    1811 8418661400    0%   /System/Volumes/Preboot
/dev/disk3s4    926Gi  623Mi  803Gi     1%     267 8418661400    0%   /System/Volumes/Update
/dev/disk1s2    500Mi  6.0Mi  480Mi     2%       1    4910360    0%   /System/Volumes/xarts
/dev/disk1s1    500Mi  7.4Mi  480Mi     2%      31    4910360    0%   /System/Volumes/iSCPreboot
/dev/disk1s3    500Mi  2.4Mi  480Mi     1%      49    4910360    0%   /System/Volumes/Hardware
/dev/disk3s5    926Gi   93Gi  803Gi    11% 1154868 8418661400    0%   /System/Volumes/Data
map auto_home     0Bi    0Bi    0Bi   100%       0          0  100%   /System/Volumes/Data/home
/dev/disk2s1    5.0Gi  1.6Gi  3.4Gi    32%      58   35784400    0%   /System/Volumes/Update/SFR/mnt1
/dev/disk3s1    926Gi   20Gi  803Gi     3%  502130 4293208963    0%   /System/Volumes/Update/mnt1

We can convert this table into json with jq

disk_report=($(df -h | tr -s ' ' | jq -c -Rn 'input  | split(" ") as $head | inputs | split(" ") | to_entries | map(.key = $head[.key]) | from_entries'))
echo $disk_report | jq

The output

{
  "Filesystem": "/dev/disk3s1s1",
  "Size": "926Gi",
  "Used": "20Gi",
  "Avail": "803Gi",
  "Capacity": "3%",
  "iused": "502068",
  "ifree": "4293294021",
  "%iused": "0%",
  "Mounted": "/"
}
{
  "Filesystem": "devfs",
  "Size": "205Ki",
  "Used": "205Ki",
  "Avail": "0Bi",
  "Capacity": "100%",
  "iused": "710",
  "ifree": "0",
  "%iused": "100%",
  "Mounted": "/dev"
}
{
  "Filesystem": "/dev/disk3s6",
  "Size": "926Gi",
  "Used": "7.0Gi",
  "Avail": "803Gi",
  "Capacity": "1%",
  "iused": "7",
  "ifree": "8418536520",
  "%iused": "0%",
  "Mounted": "/System/Volumes/VM"
}
{
  "Filesystem": "/dev/disk3s2",
  "Size": "926Gi",
  "Used": "857Mi",
  "Avail": "803Gi",
  "Capacity": "1%",
  "iused": "1811",
  "ifree": "8418536520",
  "%iused": "0%",
  "Mounted": "/System/Volumes/Preboot"
}
{
  "Filesystem": "/dev/disk3s4",
  "Size": "926Gi",
  "Used": "623Mi",
  "Avail": "803Gi",
  "Capacity": "1%",
  "iused": "267",
  "ifree": "8418536520",
  "%iused": "0%",
  "Mounted": "/System/Volumes/Update"
}
{
  "Filesystem": "/dev/disk1s2",
  "Size": "500Mi",
  "Used": "6.0Mi",
  "Avail": "480Mi",
  "Capacity": "2%",
  "iused": "1",
  "ifree": "4910360",
  "%iused": "0%",
  "Mounted": "/System/Volumes/xarts"
}
{
  "Filesystem": "/dev/disk1s1",
  "Size": "500Mi",
  "Used": "7.4Mi",
  "Avail": "480Mi",
  "Capacity": "2%",
  "iused": "31",
  "ifree": "4910360",
  "%iused": "0%",
  "Mounted": "/System/Volumes/iSCPreboot"
}
{
  "Filesystem": "/dev/disk1s3",
  "Size": "500Mi",
  "Used": "2.4Mi",
  "Avail": "480Mi",
  "Capacity": "1%",
  "iused": "49",
  "ifree": "4910360",
  "%iused": "0%",
  "Mounted": "/System/Volumes/Hardware"
}
{
  "Filesystem": "/dev/disk3s5",
  "Size": "926Gi",
  "Used": "93Gi",
  "Avail": "803Gi",
  "Capacity": "11%",
  "iused": "1155057",
  "ifree": "8418536520",
  "%iused": "0%",
  "Mounted": "/System/Volumes/Data"
}
{
  "Filesystem": "map",
  "Size": "auto_home",
  "Used": "0Bi",
  "Avail": "0Bi",
  "Capacity": "0Bi",
  "iused": "100%",
  "ifree": "0",
  "%iused": "0",
  "Mounted": "100%",
  "on": "/System/Volumes/Data/home"
}
{
  "Filesystem": "/dev/disk2s1",
  "Size": "5.0Gi",
  "Used": "1.6Gi",
  "Avail": "3.4Gi",
  "Capacity": "32%",
  "iused": "58",
  "ifree": "35784400",
  "%iused": "0%",
  "Mounted": "/System/Volumes/Update/SFR/mnt1"
}
{
  "Filesystem": "/dev/disk3s1",
  "Size": "926Gi",
  "Used": "20Gi",
  "Avail": "803Gi",
  "Capacity": "3%",
  "iused": "502130",
  "ifree": "4293208963",
  "%iused": "0%",
  "Mounted": "/System/Volumes/Update/mnt1"
}

List all Filesystem

echo $disk_report | jq '.Filesystem'

"/dev/disk3s1s1"
"devfs"
"/dev/disk3s6"
"/dev/disk3s2"
"/dev/disk3s4"
"/dev/disk1s2"
"/dev/disk1s1"
"/dev/disk1s3"
"/dev/disk3s5"
"map"
"/dev/disk2s1"
"/dev/disk3s1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment