Shell script to know whether a filesystem is already mounted

nitin_cherian picture nitin_cherian · Nov 18, 2010 · Viewed 29k times · Source

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.

Answer

sorpigal picture sorpigal · Nov 18, 2010

There's a tool specifically for this: mountpoint(1)

if mountpoint -q "$directory" ; then
    echo it is a mounted mountpoint
else
    echo it is not a mounted mountpoint
fi

And you don't even have to scrape strings to do it!

Note that I find this tool in Debian's initscripts package. How available it is elsewhere is not something I can comment on.