Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 508
nitin_cherian Avatar asked Nov 18 '10 07:11

nitin_cherian


2 Answers

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.

like image 194
sorpigal Avatar answered Oct 05 '22 23:10

sorpigal


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
like image 36
ephemient Avatar answered Oct 06 '22 00:10

ephemient