5

I want to sync an USB drive with a folder located on a NTFS partition which is not mounted at boot time. I have found a command to do that: rsync -axu /media/USB/folder /home/user/folder. But I have to have the NTFS drive already mounted.

How can I detect if a NTFS drive is mounted or not from a script and if is not to mount it? I use Linux (Ubuntu).

This is the little script modified (the original is here) with your help!

#!/bin/bash
## CONFIG SECTION
MOUNT_DRIVE=/dev/disk/by-id/scsi-SATA_Hitachi_HTS5416_SB3404GRGJRKBS-part5
MOUNT_POINT=/media/Windows/
# Local folder to sync with
SYNC_LOC=/media/Windows/vasia/Disertatie/
# Device folder to sync with
SYNC_DEV=Disertatie
#
## SCRIPT SECTION
#verify if the drives are mounted
if mount | grep -q "/media/Windows/"; then
#device is mounted
echo "Windows NTFS Drive is mounted!"
else
#device is not mounted,let's mount it
mount $MOUNT_DRIVE $MOUNT_POINT
fi
# Wait for thumbdrive to settle
sleep 10
# Synchronize thumbdrive with local
rsync -axu /media/DISK_IMG/${SYNC_DEV}/ ${SYNC_LOC}/
# Synchronize local with thumbdrive
#rsync -axu ${SYNC_LOC}/ /media/disk/${SYNC_DEV}/
# Inform user that synchronization is complete.
zenity --title "Thumbdrive Sync" \
       --info --text "File synchronization        complete."

2

There is one other solution on how you can do it. You can create a file in the NTFS file system and then check if this file exists. In your case the other solutions are probably better. But if you need to check if a NFS or SMB file system is mounted it is sometimes better to check for a file because you want to be sure that it is not only mounted but as well accessible. Then you would do it like this:

if [ ! -f /your/mounted/fs/mount-check.txt ];
then
    echo "Here you have to put your mount command."
fi
3

There are many ways to check if a particular directory is a mount point, for example (under Linux) checking in the mount point list

if ! </proc/mounts awk '$2 == "/mount/point" {exit(0)} END {exit(1)}'; then
  mount /mount/point
fi

or (portably) checking whether the path's filesystem's mount point is itself

if ! df -P /mount/point | grep -q '/mount/point$'; then
  mount /mount/point
fi

If the possible mount point may be a symbolic link, then see if that directory is on a different filesystem from its parent:

if df -P /mount/point/. /mount/point/.. | {
      read -r _; read -r dev1 _; read -r dev2 _
      [ "$dev1" = "$dev2" ]
    }; then
  mount /mount/point
fi

An alternative approach is to use an automounter to automatically mount the directory when it's accessed. For example, on Linux, you can use autofs:

  • Uncomment the line in /etc/auto.master that reads /misc /etc/auto.misc
  • In /etc/auto.misc, add windows_stuff -fstype=ntfs :/dev/sdz99 (replace sdz99 by the partition you want to mount)
  • Create a symbolic link /home/user/folder/misc/windows_stuff
  • Run /etc/init.d/autofs reload (or whatever tells autofs to reload its configuration on your distribution).
3

Use mountpoint command:

mountpoint -q $MOUNT_POINT || mount $DEVICE_WITH_NTFS_FILESYSTEM $MOUNT_POINT

The latter part (after ||) is executed only if the first part was false (i.e. the mount point did not exist).

2

You can check what is mounted by issuing a plain mount command. It also tells you the "source device" of each mount point.

If you use the devices in /dev/disk/by-id to mount your NTFS volume, then you'll have a fixed name you can check against in scripts, as opposed to the /dev/sd* names that can change between reboots and drive connect events.

So something like this can check for the presence of a mounted drive in bash:

if mount | grep -q "/dev/disk/by-id/$DEVICE_WITH_NTFS_FILESYSTEM"; then
 # device is mounted
 ;; # do nothing
else
 # device is not mounted, let's mount it
 mount $DEVICE_WITH_NTFS_FILESYSTEM $MOUNT_POINT
 fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.