entr and watch: Make Your Terminal Rerun Things So You Don't Have To
Save, switch window, press up, press Enter, squint. Save, switch window, press up... Stop. entr reruns your command whenever a file changes, and watch reruns it on a timer. Two tiny tools, a thousand saved keystrokes.
Tux Harlow
4 min read
Save the file. Switch to the terminal. Press up. Press Enter. Squint at the test results. Switch back. Change one character. Save. Switch. Up. Enter. Squint.
By lunchtime you've done this 300 times and developed a twitch. [Cut to a man at a desk, eyes wide, hand jerking between keyboard and mouse like a malfunctioning robot. His cat watches from the shelf, judging. The cat has never pressed up-Enter in its life. The cat is thriving.]
Be the cat. Let the computer press the buttons. Today's heroes are entr and watch: one reruns a command when files change, the other reruns it on a timer.
entr: run it when something changes
sudo apt install entr # Debian, Ubuntu
sudo dnf install entr # Fedora
entr has the simplest interface ever: feed it a list of files on standard input, give it a command, and it runs the command every time one of those files changes.
ls *.py | entr python test_app.py
Save any .py file and the tests run. You don't switch windows, press up or squint. Okay, you still squint. entr can't fix your eyes.
The flags you'll actually use:
ls src/*.rs | entr -c cargo test
-c clears the screen before each run, so you only see the latest results, not a scrolling wall of history.
ls *.py | entr -r python app.py
-r restarts a long-running process. Without it, entr waits for the command to finish, and a web server never finishes. With -r, every save kills the old server and starts a fresh one. Instant live reload for anything, in any language, without framework magic.
ls *.md | entr -c pandoc /_ -o preview.html
/_ is replaced with the name of the file that changed, so only that file gets rebuilt.
Pair it with fd
Big project, files in folders? Use fd (from our bat, eza and fd review) to build the list. It respects .gitignore, so entr doesn't watch node_modules and melt:
fd -e rs | entr -c cargo test
One catch: entr watches the list it was given when it started. Create a new file and entr doesn't know about it. Use -d to watch the directories too. Then, when a new file appears, entr exits, so wrap it in a loop:
while true; do fd -e rs | entr -cd cargo test; done
Ctrl-C twice to escape. Like a nightclub with two bouncers.
watch: run it every few seconds
watch is probably already installed (it comes with procps). It reruns a command on a timer and shows the output full-screen:
watch df -h
That shows disk usage refreshing every two seconds, the default. Perfect for "is this download going to fill the disk?"
watch -n 1 -d free -h
-n 1 runs it every second. -d highlights whatever changed since the last run, so changing numbers jump out at you. It's like a spot-the-difference puzzle where the computer circles the answers.
Pipes and quotes need wrapping in single quotes:
watch -n 5 'ss -tn state established | wc -l'
watch -n 10 'ls -lh /var/backups | tail -5'
The first counts open TCP connections every five seconds, which is great during a traffic spike. The second watches a backup land.
watch -g 'systemctl is-active myapp'
-g exits as soon as the output changes. Pair it with a notification and you've got "tell me when the service comes back":
watch -g 'systemctl is-active myapp'; notify-send "myapp changed state"
Now you can go and make tea instead of staring at a terminal like it owes you money.
Which one when?
| You want to... | Use |
|---|---|
| Rerun tests or builds when you save | entr |
| Restart a dev server on every change | entr -r |
| Watch a number change over time | watch |
| Spot what changed between runs | watch -d |
| Wait until something changes, then stop | watch -g |
Two tiny tools, both about one screen of documentation, and a thousand fewer up-Enter presses a day. The cat approves. The cat still won't help, but it approves.
Linux
More like this
Linux
ripgrep: grep, But It Drank Three Espressos and Read Your .gitignore
grep is a legend. It's also happy to search all 80,000 files in node_modules while you wait. ripgrep searches only what matters, very quickly, with sensible defaults. The flags worth knowing, and when old grep still wins.
Linux
Linux Terminal Toys (and the Surprisingly Useful Tools Hiding Among Them)
A talking cow, a train that punishes typos, the Matrix in your terminal, and hidden among them, six genuinely useful tools you'll keep long after the novelty wears off. A toy box for Linux nerds.
Linux
The /proc Filesystem: Your Kernel Keeps a Diary and Leaves It Open
Your kernel writes down everything: every process, every open file, how long it's been awake. It keeps it all in /proc, and it never locks the drawer. A tour of the proc filesystem, including how to rescue a file you already deleted.