Search code examples
pythonwatchdog

How to ignore files starting with a period using watchmedo?


My file editor creates temporary files prefixed with a ..

I am running:

watchmedo shell-command -p '*.py' -R -c 'echo "${watch_src_path}"'

I see events for the temporary files as I am editing, then two events on file save (presumably because it does a delete and write).

I would like to see one event -- only when I save a file.

Is there a way for me to do this with just the CLI? I am not interested in creating a python script and using the watchdog API directly.


Solution

  • Use the --ignore-patterns (-i) switch.

    watchmedo shell-command \
        -p'*.py' \
        -R \
        -c'echo "${watch_src_path}"'\
        --ignore-patterns="*/.*"
    

    Note that watchmedo is matching on the full watch_src_path so your ignore pattern can't be as simple as ".*" like you'd think at first. Also all the pitfalls of wildcards are in effect, so if you were doing something silly like working in a hidden directory /path/to/some/.hidden/dir then you'd have to have a fancier pattern.

    You also might want the --ignore-directories (-D) switch if the directory-related event is causing you annoyance too (this one is just boolean, no argument needed).