Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor directory listing for changes?

On a unix system, how do I monitor (like how 'tail' works) a directory for changes made to files - either new ones created, or size changes, etc.

Looking for a command line tool rather than something to be installed.

like image 625
siliconpi Avatar asked Oct 20 '10 12:10

siliconpi


People also ask

How do I monitor a folder for changes?

Locate the file or folder whose permission changes you wish to track. Right click on it and go to Properties. In the Security tab, click the Advanced button. In Advanced Security Settings for Active Directory window, go to Auditing tab, and click the Add button to add a new auditing entry.

How do I monitor files in a folder?

You can use the ellipsis (...) button to browse for the folder. Select this option to monitor the files and folders in sub-folders in the Folder that you specified.

How do I monitor a directory in Linux?

To test how fswatch works, open two Terminal windows (Let us call them Terminal 1 and Terminal 2). In Terminal 1, run the fswatch command to monitor the $HOME directory.


1 Answers

Most unix variants have an API for this, but it's not standardized. On Linux, there is inotify. On the command line, you can use inotifywait. Usage example:

inotifywait -m /path/to/dir | while read -r dir event name; do
  case $event in
    OPEN) echo "The file $name was created or opened (not necessarily for writing)";;
    WRITE) echo "The file $name was written to";;
    DELETE) echo "The file $name was deleted ";;
  esac
done

Inotify event types are often not exactly what you're trying to notice (e.g. OPEN is very wide), so don't feel bad if you end up making your own file checks.

like image 83
Gilles 'SO- stop being evil' Avatar answered Oct 10 '22 02:10

Gilles 'SO- stop being evil'