fsniper is one of those really neat tools I’ve found while just taking a stroll through the repositories, but for which I’ve never quite found a good use.
What fsniper does is that it watches one or more directories, for the addition of files of various mime types or that holds a file name that matches a regular expression, and executes a command or a script, aimed at manipulating that file.
Let’s try a practical use for this (thank you bio-geeks for the idea):
I have a bin directory in my home folder (i.e. ${HOME}/bin), and obviously every script I put in that directory should be executable.
watch {
$HOME/bin {
*.sh {
handler = chmod o+x %%
}
}
}
This short example shows what is needed:
- We need to know WHERE fsniper should look
- We need to know WHAT fsniper should react on
- Finally, we need to know HOW fsniper should react
Every now and then I also do a little bit of python coding, so I might want to make these executable as well.
watch {
$HOME/bin {
*.sh {
handler = chmod o+x %%
}
*.py {
handler = chmod o+x %%
}
}
}
The syntax is rather easy to work with, there are just a couple of special stuff (i.e. %% and the accompanying %f and %d)
%% will expand to mean /the/full/path/to/this/file, with %f expanding to the file name and %d expanding to /the/full/path/to/this/
You get the picture.
And as for the matching rules above, yes, I could have used regular expressions akin to /.*\.(sh|py)/ but then I wouldn’t have been able to show you how to expand on the example.
And just like I added another file name matcher, I could just as easily add another directory to watch.
So right now you are probably wondering, where should this configuration file be put? The answer is: ~/.config/fsniper/config
Then there is the small matter of running fsniper, as follows:
$ fsniper --daemon
If you start it up, and check ~/.config/fsniper again, you will notice a log file aptly named “log”. This contains some really handy output for debugging your configuration when things doesn’t do what you expect them to.
Finally, in the ~/.config/fsniper directory you can also create a subdirectory named “scripts”, and fsniper will make sure to append that directory to the $PATH variable.
Very handy for creating handler-scripts for use only with fsniper.
In closing, I just want to say that fsniper can be a bit more complex than this, acting differently based upon what the exit code of a handler was (i.e. we can engineer our scripts to fail graciously, triggering fsniper to try the next handler defined for that directory).
So if I have piqued your interest I wholeheartedly recommend that you check out the following resources:
Thanks for reading, hope you found it useful
:wq
Tags: automation, file management, fsniper
