How to find usb mounting point and serial number in bash

I’d been figuring out a way to find the mount points and their serial numbers for USB attached to the machine. So I decided to write a bash script which should return the details for multiple USB devices in a pipe (|) separated format.

/media/E4BC-1A90 :  2008012411263424  |  /media/Transcend : 0008012411263424
mount point           Serial Number

Lets do it…

JFYI, There are many commands available in Linux to find all the mounted devices on your computer but the one we are going to use is ‘df’ which basically reports file system disk space usage. Here is what you get after running the command in Terminal.

amit@codef0rmer:/var/www/side-projects/usbsData$ df
Filesystem           1K-blocks      Used                 Available           Use%         Mounted on
/dev/sda1            49213248       18053208         28660100           39%            /
udev                   1001524         4                     1001520            1%              /dev
tmpfs                 404564            992                 403572              1%              /run
none                  5120               0                     5120                  0%             /run/lock
none                  1011404         2592                1008812             1%             /run/shm
/dev/sda6           185319436      38757044         137148660         23%            /home

/dev/sdb1           2058296          4                     2058292            1%             /media/E4BC-1A90

Now we’ll have to parse the output of the above command to extract only external devices mounted which IMO, mount into /media/ directory always, and store the locations into an array for further use.

# Fetching usbs location and putting them in array
while read row
   loc=$( echo $row | grep -P '/media/.*?$' -o )
   if [ "$loc" != "" ]; then
      locArr[ $locIterator ]="$loc"
      (( locIterator++ ))
done < <(df)

There are many commands to fetch the serial number and other details of the devices as well but I found this command suitable in this case. Here we are concatenating the content of multiple files generated after mounting the external devices using ‘cat /proc/scsi/usb-storage/*’ command.

# Fetching usbs serial numbers and putting them in array
while read row
   sn=$( echo $row | grep -P 'Serial Number:.*?$' -o )
   if [ "$sn" != "" ]; then
      snArr[ $snIterator ]="${sn:15}"
     (( snIterator++ ))
done < <(cat /proc/scsi/usb-storage/*)

Finally, we are going to loop through the first array and merge its elements with that of the second array, so we’ll get the desire output (see the starting of the blog post).

# Generating Output by looping through the both arrays
for (( i = 0 ; i < ${#locArr[@]} ; i++ ))
   if [ "$i" != "0"  ]; then 
   output="$output${locArr[$i]}: ${snArr[$i]}"
echo $output


Run the script in a terminal `$ bash ./`, please do not use `sh` instead of bash as you may get some belligerent errors 😉  and of course you can download the code from github.

Last but not the least, there are many ways to achieve the same thing and no program is 100% perfect so feel free to comment If you think we can make this bash script better. Good night! |-)

/media/E4BC-1A90: 2008012411263424
If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.