You can't, given the user creating the directory has sufficient permission to write on the parent directory.
You can instead leverage the inotify
family of system calls provided by the Linux kernel, to watch for the creation (and optionally mv
-ing) of directory shop
in the given directory, if created (or optionally mv
-ed), rm
the directory.
The userspace program you need in this case is inotifywait
(comes with inotify-tools
, install it first if needed).
Assuming the directory shop
would be residing in /foo/bar
directory, let's set a monitoring for /foo/bar/shop
creation, and rm
instantly if created:
inotifywait -qme create /foo/bar | \
awk '/,ISDIR shop$/ { system("rm -r -- /foo/bar/shop") }'
inotifywait -qme create /foo/bar
watches /foo/bar
directory for any file/directory that might be created i.e. watch for any create
event
If created, awk '/,ISDIR shop$/ { system("rm -r -- /foo/bar/shop") }'
checks if the file happens to be a directory and the name is shop
(/,ISDIR shop$/
), if so rm
the directory (system("rm -r -- /foo/bar/shop")
)
You need to run the command as a user that has write permission on directory /foo/bar
for removal of shop
from the directory.
If you want to monitor mv
-ing operations too, add watch for moved_to
event too:
inotifywait -qme create,moved_to /foo/bar | \
awk '/,ISDIR shop$/ { system("rm -r -- /foo/bar/shop") }'
Just to note, if you are looking for a file, not directory, named shop
:
inotifywait -qme create /foo/bar | \
awk '$NF == "shop" { system("rm -- /foo/bar/shop") }'
inotifywait -qme create,moved_to /foo/bar | \
awk '$NF == "shop" { system("rm -- /foo/bar/shop") }'