Linux CLI – Execute a command when a file changes

The power of the CLI on Linux is something that can’t be discussed. Bash and ZSH let us do some magnificent tricks to automate our lifes and make our everyday experience easier. Today, I want to share with you one of those awesome little tricks.


Sometimes can be useful to read a file and execute some command whenever the file changes. Fortunately, this can be done with one simple line for the Linux CLI if we have inotify-tools* installed.

while inotifywait -e close_write file.txt; do ./command.sh; done

If you are using vim to edit the file and want to trigger the command everytime you write the file, you need to change the close_write paramater for delete_self. So for vim the line goes something like this

while inotifywait -e delete_self file.txt; do ./command.sh; done

(*) To install inotify on Ubuntu 16.04, just install the package inotify-tools with apt-get

sudo apt-get install inotify-tools

You can find more information about this cool trick on StackOverflow

Thanks for reading this, I hope you find this information useful. See you in other post, Bye! 🙂

Compartí el post, no seas careta...Share on twitter
Twitter
Share on facebook
Facebook
0
Share on google
Google
Share on tumblr
Tumblr
0
Share on linkedin
Linkedin
0
Buy Me A Coffee

Recent Posts

Recent Comments

Archives

Categories

Santi Written by: