-1

Let's say I have a file /local/content/en/abc.html and I have a same file in /local/en/abc.html. I want to add a watcher to /local so that if /local/content/en/abc.htmlgets deleted, it automatically deletes the /local/en/abc.htmlfile.

How can I achieve this? I have a Linux environment. This can be a bash file which would be watching/local directory.

donjuedo
  • 2,475
  • 18
  • 28
Shauzab
  • 65
  • 1
  • 4
  • 11
  • It seems like it should just be a simple `if` statement. It tests if one file exists, and if not it deletes the other file. – Barmar Jan 04 '17 at 19:32
  • Possible duplicate of [How do I tell if a regular file does not exist in bash?](http://stackoverflow.com/questions/638975/how-do-i-tell-if-a-regular-file-does-not-exist-in-bash) – Chem-man17 Jan 04 '17 at 19:33
  • Looks like a good use case for `rsync --delete` – Paulo Scardine Jan 04 '17 at 19:33
  • @Shauzab. Do they have the same content (/local/content/en/abc.html and /local/en/abc.html) ? – V. Michel Jan 04 '17 at 19:46
  • 1
    Cross-site duplicate: http://superuser.com/questions/939600/how-to-get-notified-when-a-specific-file-is-deleted-in-linux – tripleee Jan 05 '17 at 05:55

2 Answers2

5

Looks like a job for inotify which is built into the Linux kernel. This is a subsystem that lets you watch specific folders for a change and receive notifications if/when that happens. It is typically used to automatically update directory views, reload configuration files, log changes, backup, synchronize, etc.

inotify itself is an API, not a program. You can find more info on the API from the inotify man page if you want to write a small program yourself, or check out the GitHub link for a tool which lets you easily set up an inotify watch from a shell script (where you can take appropriate action when the notification comes in).

https://en.wikipedia.org/wiki/Inotify

https://github.com/rvoicilas/inotify-tools

sushpa
  • 66
  • 5
0

If they have the same content, you could use a symbolic link (ln -s). For example :

$echo Test > a
$ln -s a b
$ ll
total 4
-rw-rw-r--. 1 ...................... a
lrwxrwxrwx. 1 ...................... b -> a

The files a and b have the same content :

$ cat b
Test

If you destroy a, b is only a symbolic link:

$rm b
$cat b
cat: b: no file

After the creation of the file a, you can use again the file b :

$echo Test > a
$ cat b
Test
V. Michel
  • 1,599
  • 12
  • 14