Linux: Pretty Print Mount Output

Objective: Pretty print the output of of mount command.

This is how the output of mount command looks like by default on one of my VMs – not pleasing to the eye.

$ mount
/dev/vda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/cgroup type tmpfs (rw)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
none on /run/user type tmpfs (rw,noexec,nosuid,nodev,size=104857600,mode=0755)
none on /sys/fs/pstore type pstore (rw)
tmpfs on /var/cache/nginx type tmpfs (rw,size=32M)
systemd on /sys/fs/cgroup/systemd type cgroup (rw,noexec,nosuid,nodev,none,name=systemd)

To columnate the output, we can use a simple while loop and use printf for formatting.

$ mount | while read device word_on mount_point word_type fs_type fs_opts; do printf "%-25s %s %-25s %s %-15s %s\n" $device $word_on $mount_point $word_type $fs_type $fs_opts; done
/dev/vda1                 on /                         type ext4            (rw,errors=remount-ro)
proc                      on /proc                     type proc            (rw,noexec,nosuid,nodev)
sysfs                     on /sys                      type sysfs           (rw,noexec,nosuid,nodev)
none                      on /sys/fs/cgroup            type tmpfs           (rw)
none                      on /sys/fs/fuse/connections  type fusectl         (rw)
none                      on /sys/kernel/debug         type debugfs         (rw)
none                      on /sys/kernel/security      type securityfs      (rw)
udev                      on /dev                      type devtmpfs        (rw,mode=0755)
devpts                    on /dev/pts                  type devpts          (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs                     on /run                      type tmpfs           (rw,noexec,nosuid,size=10%,mode=0755)
none                      on /run/lock                 type tmpfs           (rw,noexec,nosuid,nodev,size=5242880)
none                      on /run/shm                  type tmpfs           (rw,nosuid,nodev)
none                      on /run/user                 type tmpfs           (rw,noexec,nosuid,nodev,size=104857600,mode=0755)
none                      on /sys/fs/pstore            type pstore          (rw)
tmpfs                     on /var/cache/nginx          type tmpfs           (rw,size=32M)
systemd                   on /sys/fs/cgroup/systemd    type cgroup          (rw,noexec,nosuid,nodev,none,name=systemd)

The same output can be achieved using awk using the following syntax.

$ mount | awk '{ printf  "%-25s %s %-25s %s %-15s %s\n", $1, $2, $3, $4, $5, $6 }'

The final method is to use the column command – a utility to columnate lists.

$ mount | column -t
/dev/vda1  on  /                         type  ext4        (rw,errors=remount-ro)
proc       on  /proc                     type  proc        (rw,noexec,nosuid,nodev)
sysfs      on  /sys                      type  sysfs       (rw,noexec,nosuid,nodev)
none       on  /sys/fs/cgroup            type  tmpfs       (rw)
none       on  /sys/fs/fuse/connections  type  fusectl     (rw)
none       on  /sys/kernel/debug         type  debugfs     (rw)
none       on  /sys/kernel/security      type  securityfs  (rw)
udev       on  /dev                      type  devtmpfs    (rw,mode=0755)
devpts     on  /dev/pts                  type  devpts      (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs      on  /run                      type  tmpfs       (rw,noexec,nosuid,size=10%,mode=0755)
none       on  /run/lock                 type  tmpfs       (rw,noexec,nosuid,nodev,size=5242880)
none       on  /run/shm                  type  tmpfs       (rw,nosuid,nodev)
none       on  /run/user                 type  tmpfs       (rw,noexec,nosuid,nodev,size=104857600,mode=0755)
none       on  /sys/fs/pstore            type  pstore      (rw)
tmpfs      on  /var/cache/nginx          type  tmpfs       (rw,size=32M)
systemd    on  /sys/fs/cgroup/systemd    type  cgroup      (rw,noexec,nosuid,nodev,none,name=systemd)

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }