RSync only if filesystem is mounted RSync only if filesystem is mounted bash bash

RSync only if filesystem is mounted


mountpoint seems to be the best solution to this: it returns 0 if a path is a mount point:

#!/bin/bashif [[ `mountpoint -q /path` ]]; then    echo "filesystem mounted"else    echo "filesystem not mounted"fi

Found at LinuxQuestions.


if cut -d' ' -f2 /proc/mounts | grep '^/mnt/remote1$' >/dev/null; then    rsync -avz ...fi

Get the list of mounted partitions from /proc/mounts, only match /mnt/remote1 (and if it is mounted, send grep's output to /dev/null), then run your rsync job.

Recent greps have a -q option that you can use instead of sending the output to /dev/null.


A quick google led me to this bash script that can check if a filesystem is mounted. It seems that grepping the output of df or mount is the way to go:

if df |grep -q '/mnt/mountpoint$'    then        echo "Found mount point, running task"        # Do some stuff    else        echo "Aborted because the disk is not mounted"        # Do some error correcting stuff        exit -1fi