I want a device mounted to a mountpoint only when it is needed, when it is accessed (for the first time). How to run a script when a folder is accessed in Linux (or Ubuntu)? Please note that the device is an encrypted container and the script will ask the user for a password before it mounts.

  • 1
    Mounting a directory on first use is called automounting. Don't try to run a script when the directory is accessed: that wouldn't work, because you'd mount the directory after the application has already opened it and found it empty. An automounter works differently: it's always running, and the first access to the directory blocks until the mounting is done. Asking for a password from an automounter is not so easy. What encryption mechanism is it? – Gilles 'SO- stop being evil' Aug 29 '19 at 13:03
  • Presently my mount script is similar to this: password=$(kdialog --password 'Enter your passphrase' --title='Container passphrase') echo -n $password|sudo /sbin/cryptsetup luksOpen $CONTAINER $MOUNT sudo mount /dev/mapper/$MOUNT ~/$MOUNT I think that automount would not make much sense here because the container file is not on a removible device and simply sits on my harddisk. – ñull Sep 10 '19 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.