Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm triggering a script from cron. I want it to run rsync ONLY if a USB disk called "data_3" is mounted, otherwise not.

How do I write a bash script to detect a mounted USB drive ?

My current pseudocode:

if ( mount | grep /media/data_3 )  
  rsync ...  
  echo "Failure"
share|improve this question
up vote 1 down vote accepted


if mount | grep -q ' on /media/data_3 '; then

Don't forget the fi at the end (help if for details).

share|improve this answer

You could also do it another way - find out the uuid of the disk by issuing the command when that usb is inserted first time (the objective is to find out the uuid) by using vol_id

NAME vol_id - probe filesystem type and read label and uuid

SYNOPSIS vol_id [--export] [--type] [--label] [--label-raw] [--uuid] [--skip-raid] [--probe-all] [--offset=bytes] [--debug] [--help] [device]

DESCRIPTION vol_id is usually called from a udev rule, to provide udev with the filesystem type, the label and the uuid of a volume. It supports most of the common filesystem formats and detects various raid setups to prevent the recognition of raid members as a volume with a filesystem.

OPTIONS --export Print all values in key/value format to import them into the environment.

       Print the filesystem type.

       Print the safe version of volume label suitable for use as


       Print the raw volume label.

       Print the uuid of a volume.

       Skip detection of raid metadata.

       Probe for all types and print all matches.

       Start probing at the given offset, instead of the beginning of

the volume. The offset value is specified in bytes.

       Print debug messages to stderr.

       Print usage.

ENVIRONMENT UDEV_LOG Set the syslog priority.

EXIT STATUS vol_id will only return successful if the value asked for is not empty. All trailing whitespace will be removed, spaces replaced by underscore and slashes ignored.

Then it's a matter of checking the vol_id once you have the value for that disk...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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