# how to check if a filesystem is mounted?



## fluca1978 (Nov 10, 2011)

Hi,
this is a quite trivial question: in a lot of scripts I test if some removable media is mounted at a specific point in the filesystem. I do this with something like the following:


```
mount | grep $MOUNT_POINT | wc -l
```

and test how many lines I got back. Something smaller is


```
mount | grep $MOUNT_POINT
```

and test the return value of grep. If I want to test that a device is mounted exactly in a mount point I do something like:


```
mount | grep $MOUNT_POINT | grep $DEVICE
```


Is there a smarter way or some utility command to test if a filesystem is mounted in a specific position?


----------



## hedgehog (Nov 10, 2011)

something like this maybe?

```
mount |egrep "$DEVICE.*$MOUNT_POINT"
```


----------



## fluca1978 (Nov 10, 2011)

hedgehog said:
			
		

> something like this maybe?
> 
> ```
> mount |egrep "$DEVICE.*$MOUNT_POINT"
> ```



Despite this is smaller than my method, and therefore more elegant, it is based on the same "engine". I was thinking about a mount option or a command that can do something like:


```
mount --check $DEVIVE $MOUNTPOINT
```

or something alike.


----------



## mky (Nov 11, 2011)

You can use df(1) command, giving a path or a device as parameter and checking the return value. For example:


```
df / ; echo The return value is $?
Filesystem   1K-blocks     Used    Avail Capacity  Mounted on
/dev/ada0s3a  90822300 18912476 64644040    23%    /
The return value is 0
```


```
df /blah ; echo The return value is $?
df: /blah: No such file or directory
The return value is 1
```


```
df /dev/ada0s3a ; echo The return value is $?
Filesystem   1K-blocks     Used    Avail Capacity  Mounted on
/dev/ada0s3a  90822300 18916248 64640268    23%    /
The return value is 0
```


```
df /dev/ada0s3f ; echo The return value is $?
df: /dev/ada0s3f: No such file or directory
The return value is 1
```


----------



## fluca1978 (Nov 11, 2011)

mky said:
			
		

> You can use df(1) command, giving a path or a device as parameter and checking the return value.



This is a good advice, thanks!


----------



## jalla (Nov 11, 2011)

df(1)() works very nicely, yes. Use it simply like this

```
df /path | grep -q path || mount /path
```


----------



## fluca1978 (Nov 11, 2011)

Thanks, combining df and mount is the natural evolution and helps me reaching my aim.


----------



## wblock@ (Nov 11, 2011)

Sorry, I don't see it.  How is df/grep/mount better than hedgehog's mount/grep from post #2?


----------



## fluca1978 (Nov 12, 2011)

wblock@ said:
			
		

> Sorry, I don't see it.  How is df/grep/mount better than hedgehog's mount/grep from post #2?



You are right, it is not better. But having df to directly report in the exit status if the path is mounted is, in my opinion, better than having to grep the mount command.


----------

