Is it possible to reliably check with a shell-script if a directory was deleted and later recreated with the same name in Linux? Depending on the filesystem one could check for a change in the inode-number of the directory in question. I am unhappy with this solution though because it does not work if the same inode-number will get reused sooner or later by the filesystem and therefore I am looking for a better alternative.
1 Answer
There is a way, but it might not suit your use case. Keep it open. In a shell script, this may mean cd-ing into the directory. While a directory is open, it can't be completely deleted. Once the directory is deleted, its link count will change from 2 to 0.
Consider this script:
#!/bin/bash mkdir watched_directory cd watched_directory echo directory `pwd` exists while test `stat -c%h .` -gt 0 do sleep 1 done echo 'directory is gone!' It will finish when you externally remove the directory.
stat -c %w <path/to/dir>(or%Wfor Unix timestamp)