Search code examples
shellfilesystemsmount

Shell script to know whether a filesystem is already mounted


I have a tmpfs file system mounted on a particular directory. I want to write a shell script to check whether the tmpfs filesystem is already mounted on the directory.


Solution

  • You can check the type of the filesystem.

    $ stat -f -c '%T' /
    xfs
    $ stat -f -c '%T' /dev/shm
    tmpfs
    

    You could also check whether a directory is a mountpoint by comparing its device with its parent's.

    $ stat -c '%D' /
    901
    $ stat -c '%D' /home
    fe01
    $ stat -c '%D' /home/$USER
    fe01