mr.proton
mr.proton

Reputation: 1053

How to watch and report for file creations in a specific directory?

I'm running a program which is creating and deleting files in a specific directory. Another program is trying to read those files and perform operations on them but it's always saying that the specific file cannot be found.

When manually looking for those files with "ls -l", I can see some of them but I don't really have a complete overview about all files.

In order to debug the whole thing, I need to have a report which tells me that a file was created/deleted in this directory. The files are created in sub-folders so the solution must be able to traverse the directory until the file is found.

I think the unix "watch" command could help but I have no idea how to go recursively into the directories.

Any idea?

Upvotes: 0

Views: 366

Answers (2)

Toby Speight
Toby Speight

Reputation: 30709

inotifywait is in the package inotify-tools. You can invoke it simply with your directory as argument. Here's a sample

$ inotifywait -m .
Setting up watches.
Watches established.
./ CREATE .#34003250.cpp
./ MODIFY 34003250.cpp
./ OPEN 34003250.cpp
./ MODIFY 34003250.cpp
./ CLOSE_WRITE,CLOSE 34003250.cpp
./ DELETE .#34003250.cpp
./ OPEN 33997523.txt
./ CLOSE_NOWRITE,CLOSE 33997523.txt
./ OPEN 33997523.txt
./ ACCESS 33997523.txt
./ CLOSE_NOWRITE,CLOSE 33997523.txt

The output can be formatted and filtered quite effectively - the man page has full details. It can watch (recursive) subdirectories too, but beware the warning:

-r, --recursive

Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Symbolic links are not traversed. Newly created subdirectories will also be watched.

Warning: If you use this option while watching the root directory of a large tree, it may take quite a while until all inotify watches are established, and events will not be received in this time. Also, since one inotify watch will be established per subdirectory, it is possible that the maximum amount of inotify watches per user will be reached. The default maximum is 8192; it can be increased by writing to /proc/sys/fs/inotify/max_user_watches.

Upvotes: 1

Giuseppe Ricupero
Giuseppe Ricupero

Reputation: 6272

Maybe you need to re-design your algorithm, in the meantime something like the following may help:

watch -n 1 tree /path/to/your/directory

Use sudo apt-get install tree if you are on a debian-like system or the equivalent if the tree executable is not available by default.

NB watch accept a refresh rate (passed with the -n switch) till 0.1 seconds

Upvotes: 0

Related Questions