How to monitor a complete directory tree for changes in Linux?

后端 未结 8 2047

How can I monitor a whole directory tree for changes in Linux (ext3 file system)?

Currently the directory contains about half a million files

相关标签:
8条回答
  • 2020-12-02 10:24

    Use inotifywait from inotify-tools:

    sudo apt install inotify-tools

    Now create a script myscript.sh that includes hidden files and folders too:

    #!/bin/bash
    while true; do
    
    inotifywait -e modify,create,delete,move -r $1
    
    done
    

    Make the script executable with chmod +x myscript.sh

    Run it with ./myscript.sh /folder/to/monitor

    If you don't provide argument it will use the working directory by default.

    Also, you can run several commands adding && \ at the end of the previous command to add the next one:

    #!/bin/bash
    while true; do
    
    inotifywait -e modify,create,delete,move -r $1 && \
    echo "event" && \
    echo "event 2"
    
    done
    

    If you don't want to execute any command on events, just run the command directly with the -m modifier so doesn't close:

    inotifywait -e modify,create,delete,move -m -r /path/to/your/dir

    0 讨论(0)
  • 2020-12-02 10:25

    I've done something similar using the inotifywait tool:

    #!/bin/bash
    while true; do
    
    inotifywait -e modify,create,delete -r /path/to/your/dir && \
    <some command to execute when a file event is recorded>
    
    done
    

    This will setup recursive directory watches on the entire tree and allow you to execute a command when something changes. If you just want to view the changes, you can add the -m flag to put it into monitor mode.

    0 讨论(0)
提交回复
热议问题