Given a directory, I'd like to know whether the files in the directory have been modified or not. (Boolean) i.e. if the directory's state has changed from before.
I don't want to run a file watcher service for this as I don't need to know which file has been modified (or if many files change receive many events)
I've looked at atime
, mtime
, ctime
from stat
eg: for a dir named taskmaster
which already contains sample.txt
stat taskmaster
output
File: taskmaster
Size: 245760 Blocks: 480 IO Block: 4096 directory
Device: 802h/2050d Inode: 1309314 Links: 1
Access: (0777/drwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-05-22 21:25:06.226421200 +0530
Modify: 2020-05-22 21:25:06.222175900 +0530
Change: 2020-05-22 21:25:06.222175900 +0530
Birth: -
After I modify the folder contents
# modify an existing file
echo modify > taskmaster/sample.txt
stat taskmaster
gives
File: taskmaster
Size: 245760 Blocks: 480 IO Block: 4096 directory
Device: 802h/2050d Inode: 1309314 Links: 1
Access: (0777/drwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-05-22 21:25:06.226421200 +0530
Modify: 2020-05-22 21:25:06.222175900 +0530
Change: 2020-05-22 21:25:06.222175900 +0530
Birth: -
The exact same output.
If no file is removed or deleted the access and modify times do not change. How can I achieve this?
I think you need to do stat
on individual files, something like this :
previous="$(stat *)"
while sleep 60; do
current="$(stat *)"
if [[ $current != $previous ]]; then
echo "Some files changed."
fi
previous=$current
done