Quick Tip: Looping a command in bash

I recently came across the need to watch my disk space while running a slow program to make sure I didn’t run out. If I did, the command would fail silently and I’d have to start over again.

This can easily be done with this short snippet:

watch -n10 bash -c "df -h | ack disk1s5"Code language: JavaScript (javascript)
Every 10.0s: bash -c df -h | ack disk1s5                                                                                  mbp.local: Mon Jul 20 15:09:51 2020

/dev/disk1s5           488245288  10940872 144033352     8%   488237 4881964643    0%   /

The important part here is disk1s5, which is the device file for the partition I wanted to watch. If you need to find this, it can be done simply by running the df as a whole:

$ df
Filesystem             1K-blocks      Used Available Capacity  iused      ifree %iused  Mounted on
/dev/disk1s5           488245288  10940872 144035124     8%   488237 4881964643    0%   /
devfs                        191       191         0   100%      662          0  100%   /dev
/dev/disk1s1           488245288 331456068 144035124    70%  1379027 4881073853    0%   /System/Volumes/Data
/dev/disk1s4           488245288   1048596 144035124     1%        1 4882452879    0%   /private/var/vm
map auto_home                  0         0         0   100%        0          0  100%   /System/Volumes/Data/home
/dev/disk1s3           488245288    516448 144035124     1%       48 4882452832    0%   /Volumes/RecoveryCode language: PHP (php)

That is all.

Photo by Wendy Wei from Pexels

Other Posts Not Worth Reading

Hey, You!

Like this kind of garbage? Subscribe for more! I post like once a month or so, unless I found something interesting to write about.