I have a wifi access point that allows to plug a USB hard drive accessible from any computer sharing the same wifi. I would like to know if there was a way a shell script could test if something is plugged there or not.

If there is a way to check which USB key or hard drive is plugged would be a delight, but I'm not asking that much.


The goal is to check if the drive is on before I can work with the files. I want to access the drive using samba. The IP is I can get the name of the drive, but the IP is fix, whichever drive I plug.

NOTE: my final goal is to upload files on that drive with a shell script. I don't mind if you give me another way to do it: SSH, FTP, HTTP, morse code, …

  • How do you access the drive? Samba? http?
    – January
    Jul 10 '13 at 8:29
  • samba, I'm adding details to the question.
    – SteeveDroz
    Jul 10 '13 at 9:06
  • Add some more details. How is the share called? Do you access it over a specific IP? Hostname? Is the name of the share constant, or do you have to browse your Windows network? etc. etc.
    – January
    Jul 10 '13 at 9:12

You're probably not going to be able to figure out which drive it is without some fiddling around. More on that later.

Firstly, try to mount the device. If the mount fails, nothing is plugged in. Change the paths and options, obviously. (must be run as root, or with sudo)

mount -t cifs -o <options here> // /mnt/whatever
if [[ $? = 1 ]]; then
   echo "Drive not available."
   exit 1;

If it succeeds, continue on with your copies to /mnt/whatever.

If you want to be able to tell which drive is inserted, one simple thing you could do is create a file in the root of the USB key or drive that identifies it. I have a Corsair USB key, for example, create an empty file called "CORSAIR" in the top directory of the key. Obviously, you could call it anything you want. Then, after mounting, test for the existence of the file. You could even unmount it at that point and mount it on another directory for further use.

if [[ -f /mnt/whatever/CORSAIR ]]; then
    echo "CORSAIR USB key detected.."
    # do some stuff
if [[ -f /mnt/whatever/PERSONAL ]]; then
    echo "Personal USB key detected..."
    umount /mnt/whatever
    mount -t cifs -o <options> // /mnt/PERSONAL

Since you're mounting and unmounting, you'll have to run as root or use "sudo mount", which will prompt for passwords.

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.