0

I use linux mint and I want a text file to reflect the changes of files in a directory, it should contain the names of files that exist in the directory and add newly created files automatically. I referred some links and it said a good way to do it is by using the inotify-tools. I did look up on that and came up with this bash script.

#!/bin/sh
while inotifywait -qqe modify path-of-directory ; do 
ls > path-of-text-file;
done

But it doesn't seem to work, Kindly guide me on how to go about it and is it enough if I just add this script to the list of start-up applications to keep it running all the time or should I use a crontab?

Nobody
  • 103
  • 6

1 Answers1

0

This manual states that modify triggers when a file is written to. You probably want to monitor create, delete and move events.

Check this more elaborate answer to similar question. There's nohup command mentioned there. Another useful command might be disown.


I think looping inotifywait may miss an event when it occurs during the execution of ls (because there's no inotifywait running at this exact moment). There are -m and -d options, they should miss nothing. This approach should be a little more reliable:

inotifywait -me create,delete,move path-of-directory | while read a ; do ls > path-of-text-file; done

Now inotifywait -m works all the time and outputs one line of text per event caught. Then every such a line causes ls to be run.


Another solution will be iwatch and its daemon. Of course setting up a daemon requires root access. Read my answer to another question and adjust it to your needs.

Additionally you may be interested in LoggedFS - Filesystem monitoring with FUSE.

  • Thanks buddy, the above works perfectly just one issue it does update the text file when a file is created but it does not recognize when the file is removed through the delete button is keyboard although it does work when rm command is used. – Nobody May 29 '17 at 12:48
  • @Nobody The delete button may mv the file to trash or so. Try adding a move event to the list (i.e. it will be create,delete,move). Answer updated. – Kamil Maciorowski May 29 '17 at 12:57