0

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.

4
  • 2
    You can check the creation time itself on recent Linux systems: stat -c %w <path/to/dir> (or %W for Unix timestamp) Commented Aug 20, 2024 at 12:27
  • @muru You are right this might already be a simple and more reliable alternative. I was thinking too complicated. Commented Aug 20, 2024 at 12:38
  • Why do you want to do this? Please make sure there is no XY problem here. Edit the question if there is. Commented Aug 20, 2024 at 13:58
  • 1
    @KamilMaciorowski it’s not possible to check if your problem is XY as I describe on your linked meta question here. If requirement is unusual and difficult to achieve then it’s better to point this out and let the OP work out how much to back up and re-think. Ironically telling someone their question may be XY gives them little to work with. Commented Aug 20, 2024 at 14:20

1 Answer 1

2

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.