On a backup process the program has to copy tar.gz file to a mounted USB flash drive. The program (nodeJS) uses API child_process.exec() to run the shell script. The function itself obtains the stdio.

So, using a shell script, how can I check whether the USBFlashDrive is disconnected/unplugged?

The device usually appears as sd[a-z]1 - please note the regex in the string.

  • 2
    What exactly do you want to do? Do you want to check whether the USB drive is plugged in, or mounted, or actually not plugged in? What information do you have about the USB stick – is it just one specific drive? Or do you rather want to test a specific mount point for a drive to be mounted there? Please edit your question and clarify, do not use comments.
    – dessert
    May 16, 2018 at 10:38
  • 1
    I would recommend that you find and use the UUID of the partition on the target device (on the USB flash drive). It is unique, and you can use it for a reliable identification, and then check if that partition is mounted, if it is connected and needs mounting, or not available at all (unplugged). -- A weaker alternative is to use the label (but maybe more convenient).
    – sudodus
    May 16, 2018 at 11:30
  • @sudodus Thank you for the answer. I don't want to find unique USB device. Any flash drive connected to the usb port would be the correct one. So I want to check if any usb device is connected, not certain usb.
    – Hairi
    May 16, 2018 at 13:21
  • 1
    What about other USB drives (for example a USB HDD or USB SSD)? I guess you would not want to write to them (or maybe that would be OK too). And there can be more than one partition. How would you tell the difference?
    – sudodus
    May 16, 2018 at 13:25
  • 2
    Maybe creating a label on the target partition in each of the USB drives, that you want to use, and mount it according to the label. You can use the same label on all the USB drives, that you want to use for this purpose, and it will work when each of them is connected alone. But if you connect more than one of them at the same time, there might be confusion (you may not know which of the drives that will be the target).
    – sudodus
    May 16, 2018 at 13:33

3 Answers 3


I suggest


according to its author,

lsusb is a utility for displaying information about USB buses in the system and the devices connected to them.

An example:

hani@My-FRIEND:~$ lsusb
Bus 001 Device 002: ID 8087:8001 Intel Corp.  
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub  
Bus 002 Device 005: ID 8087:07dc Intel Corp.   
Bus 002 Device 004: ID 04f2:b3a3 Chicony Electronics Co., Ltd   
Bus 002 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller  
Bus 002 Device 007: ID 090c:1000 Silicon Motion, Inc. - Taiwan (formerly Feiya Technology Corp.) Flash Drive  
Bus 002 Device 002: ID 0458:0185 KYE Systems Corp. (Mouse Systems)   
Bus 002 Device 006: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma   Ivory  
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

So as you see, the seventh line of the output shows details about the flash drive (USB drive) that is plugged in right now to the computer (via the USB port).


To check which USB disks are connected look in /dev/disk/by-path/

ls -l /dev/disk/by-path/*usb*  | grep -v "part" | awk '{print $NF}'|  awk -F "/" '{print $NF}' | sort

The output of previous command will list only usb disks.

grep -v "part" - excludes the partitions.


Then lsblk command can print some useful info like model and size.

for usb in $(ls -l /dev/disk/by-path/*usb*  | grep -v "part" | awk '{print $NF}'|  awk -F "/" '{print $NF}')
   lsblk  -n -d -o NAME,MODEL,VENDOR,SIZE,RM /dev/$usb

To check where the partitions of USB disks are mounted

for usbp in $(ls -l /dev/disk/by-path/usb  | grep "part" | awk '{print $NF}'|  awk -F "/" '{print $NF}')
   findmnt -rno TARGET /dev/$usbp


I don't know much about nodeJS (I am a C++ guy) but you can check the presence of drive in /dev/ folder.

When I plugged in my pen drive, I get file /dev/sdc. If I unplugged it then /dev/sdc vanishes .

Here is some more info about How to check if a file exists in a shell script

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .