How to check if a filesystem is mounted with a script

All we need is an easy explanation of the problem, so here it is.

I am new at scripting … I can do very basic stuff, but now I need a hand.

I have a local filesystem that only will be mounted when I need to do a backup.

I’m starting with this.

export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
  echo "It's mounted."
  echo "It's not mounted."; then
  mount $MOUNT;

As I said, I’m very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.

       mount has the following return codes (the bits can be ORed):
       0      success
       1      incorrect invocation or permissions
       2      system error (out of memory, cannot fork, no more loop devices)
       4      internal mount bug
       8      user interrupt
       16     problems writing or locking /etc/mtab
       32     mount failure
       64     some mount succeeded

I don’t know how to check that. Any guidance?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:

if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
    echo "$1 is not a mountpoint"

Method 2

You can check the status code of mount, and most well written executables, with the shell special parameter ?.

From man bash:

? Expands to the exit status of the most recently executed foreground pipeline.

After you run the mount command, immediately executing echo $? will print the status code from the previous command.

# mount /dev/dvd1 /mnt
  mount: no medium found on /dev/sr0
# echo $?

Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that’s not always the case.

To expand on (and correct) your example script, I added a nested if construct for clarity. It’s not the only way to test the status code and perform an action, but it’s the easiest to read when learning.


if grep -qs "$mount" /proc/mounts; then
  echo "It's mounted."
  echo "It's not mounted."
  mount "$mount"
  if [ $? -eq 0 ]; then
   echo "Mount success!"
   echo "Something went wrong with the mount..."

For more information on “Exit and Exit Status”, you can refer to the Advanced Bash-Scripting Guide.

Method 3

One more way:

if findmnt ${mount_point}) >/dev/null 2>&1 ; then
  #Do something for positive result (exit 0)
  #Do something for negative result (exit 1)

Method 4

The easiest way which doesn’t require root is:

if $(df | grep -q /mnt/ramdisk); then


or to see if it isn’t mounted:

if ! $(df | grep -q /mnt/ramdisk); then


Method 5

Short statements

Check if mounted:

mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"

Check if not mounted:

mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"

Method 6

I have tried with below script

echo "enter the file system to check whether its mounted or not"
read p
echo $p
for i in `cat /proc/mounts`
if [[ $p =~ $i ]]
echo "$p is mounted"
echo "$p is not mounted"

Only input you need to give is name of filesystem

Method 7

I try the following and it works.
It is basic, I hope I can help you.

for i in /tmp/demo.txt /etc/demo.xt /var/demo.txt /var/log/demo.txt /opt/demo.txt /demo.txt
touch $i
if [ -f $i ];
rm $i
echo "FS bien! $i"
message="FS es R/O!"
echo '${message} $i' 

Script output.

FS es R/O! /tmp/demo.txt
FS bien! /etc/demo.xt
FS bien! /var/demo.txt
FS bien! /var/log/demo.txt
FS bien! /opt/demo.txt
FS bien! /demo.txt

Method 8

Yes you can check the return status of mount. The problem is, if the filesystem is already mounted and you try to mount it again, mount will return error 32. Strictly speaking the mount failed because it was already mounted. To get around that, for example for a one-liner for a root cron backup job I would do this:

mount /myfilesystem 2>/dev/null; grep -qs '/myfilesystem ' /proc/mounts && backupscript && umount /myfilesystem

So essentially just mount the filesystem, then verify it’s mounted and if so, run the backup script. Not super-elegant, but quick and dirty and gets the job done.

Yes I know this is an ancient question, and the OP by now is probably senior sysadmin, but I had the same question and wanted to see if I could find a better solution.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply