home ~ projects ~ socials

Print A File In The Terminal Every Time It Changes

This uses fswatch. I've since converted over to using watchexec which I find more robust.

TL;DR

Use this to output a file every time it changes

fswatch -0 ~/path/to/file.txt | xargs -0 -n1 cat

Some apps will update when files change, but lots of times they'll only do that if they are in focus. This command prints the file out to the terminal every time it changes.

Notes

  • You can pass regular expressions in instead of a single file
  • You can pass in multiple paths if you want to watch multiple files or directories
  • fswatch sometimes fires twice for a single update. This confused me a few times until I figured it out
  • It's possible to add an -event Updated flag to only watch for updates, but that didn't stop firing twice
  • I see the firing twitch from NeoVim. Might happen in other apps too
  • The -0 mean when multiple files are passed they are split by the NULL character. This is done to mitigate problems with spaces in file names

TODO

    Consolidate all the posts about `fswatch`

-- end of line --