Suppose do you want test if /mnt/disk is a mount point in a shell script. How do you do this?
for mountedPath in `mount | cut -d ' ' -f 3`; do
if [ "${mountedPath}" == "${wantedPath}" ]; then
exit 0
fi
done
exit 1
if mount | cut -d ' ' -f 3 | grep '^/mnt/disk$' > /dev/null ; then
...
fi
EDIT: Used Bombe's idea to use cut.
TESTDIR=/mnt/disk
if grep -q "^[^ ]* ${TESTDIR} " /etc/mtab; then
echo mounted
else
echo not mounted
fi
To check if it's mounted:
mount | awk '{ print $1 }' | grep /dir
To check if it's mountable:
cat /etc/fstab | egrep -v '^\s*($|[#])' | awk '{ print $2 }' | grep /dir
Using GNU find
find -maxdepth 0 -printf "%D"
will give the device number of the directory. If it differs between
the directory and its parent then you have a mount point.
Add /. onto the directory name if you want symlinks to different filesystems to count as mountpoints (you'll always want it for the parent).
Disadvantages: uses GNU find so less portable
Advantages: Reports mount points not recorded in /etc/mtab.
Here is a variant with "df -P" which is supposed to be portable:
mat@owiowi:/tmp$ f(){ df -P | awk '{ if($6 == "'$1'")print }' ; }
mat@owiowi:/tmp$ f /
/dev/mapper/lvm0-vol1 20642428 17141492 2452360 88% /
mat@owiowi:/tmp$ f /mnt
mat@owiowi:/tmp$ f /mnt/media
/dev/mapper/lvm0-media 41954040 34509868 7444172 83% /mnt/media
I discover that on my Fedora 7 there is a mountpoint command.
From man mountpoint:
NAME
mountpoint - see if a directory is a mountpoint
SYNOPSIS
/bin/mountpoint [-q] [-d] /path/to/directory
/bin/mountpoint -x /dev/device
Apparently it come with the sysvinit package, I don't know if this command is available on other systems.
[root@myhost~]# rpm -qf $(which mountpoint)
sysvinit-2.86-17
Not relying on mount
, /etc/mtab
, /proc/mounts
, etc.:
if [ `stat -fc%t:%T "$dir"` != `stat -fc%t:%T "$dir/.."` ]; then
echo "$dir is mounted"
else
echo "$dir is not mounted"
fi
When $dir
is a mount point, it has a different device number than its parent directory.
The benefit over the alternatives listed so far is that you don't have to parse anything, and it does the right thing if dir=/some//path/../with///extra/components
.
The downside is that it doesn't mark /
as a mountpoint. Well, that's easy enough to special-case, but still.
I'm writing a simple script for unmounting file systems on a hard drive, off a eSATA hard drive dock. file systems i'm unmounting are ext3, bind mounts, encfs, and bindfs fuse-mounts. my old motherboard doesn't have eSATA ports nor has BIOS support for eSATA, I'm using an eSATA bracket with eSATA cable. The current gnome nautilus doesn't detect and automount nor eject my eSATA device from its gui.
i've tried the stat -fc%t:%T command and it doesn't work on my linux mint 8 x86_64 box.
mountpoint command and find -maxdepth 0 -printf "%D" both have problem seeing non-public fuse mount points created by my encfs mounts.
find -maxdepth 0 -printf "%D" and df -P aren't seeing my bind mount points (or fuse bindfs mount points), and I would need to umount all of these to spin-down and undock the hard drive.
this is messy, for bind mount points i have the mountpoint command, for detecting ext2 mount points, i can use find -maxdepth 0 -printf "%D" or the mountpoint command. for unmounting private encfs and bindfs fuse mounts from root, it leaves me greping for /etc/mtab or outputs from mount command.