LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2009, 02:55 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Extracting USB device names from /dev/disks/


Hello,

I am trying to write a shell script which will list all the USB devices (storage media) present on the system. So far I can get name of the USB devices (/dev/sd*) with the following command:
Code:
find /dev/disk/by-path/ -type l -iname \*usb\*scsi\* -print0 | xargs -0 -iD readlink -f D  | grep -v '[0-9]'
which returns the following output:
Code:
/dev/sdd
/dev/sdc
/dev/sdb
However I would also like to prefix the above output with their model name/number. Here is what my /dev/disk/by-id looks like:
Code:
debian:~# ls -l /dev/disk/by-id/ | grep usb | grep -v part
lrwxrwxrwx 1 root root  9 2009-01-30 12:31 usb-JMicron_USB_to_ATA.ATAPI_Bridge_152D203380B6-0:0 -> ../../sdb

lrwxrwxrwx 1 root root 10 2009-01-30 12:32 usb-LITE-ON_DVDRW_SHM-165P6S_DEF10CB84245-0:0 -> ../../scd1

lrwxrwxrwx 1 root root  9 2009-01-30 12:31 usb-SanDisk_uSSD_5000_20052444330A224035EA-0:0 -> ../../sdc

lrwxrwxrwx 1 root root  9 2009-01-30 12:31 usb-WDC_WD80_0BB-00CAA1_0B00012105E7C8E5-0:0 -> ../../sdd
debian:~#
Now ideally I would like to extract the model name (and number if possible) for each of the usb devices (/dev/sd*) and display it like this:
Code:
/dev/sdd usb-WDC_WD80
/dev/sdc usb-SanDisk_uSSD
/dev/sdb usb-JMicron_USB_to_ATA.ATAPI_Bridge
 
Old 01-30-2009, 04:51 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
#!/bin/bash
devlist=$(find /dev/disk/by-path/ -type l -iname \*usb\*scsi\* -print0 | xargs -0 -iD readlink -f D  | grep -v '[0-9]')
for device in $(echo $devlist)
do
  long_name=$(grep ${device##*/}$ testfile | awk '{print $8}')
  echo "${device} ${long_name%%_[0-9]*}"
done
 
Old 01-30-2009, 05:15 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by colucix View Post
Code:
#!/bin/bash
devlist=$(find /dev/disk/by-path/ -type l -iname \*usb\*scsi\* -print0 | xargs -0 -iD readlink -f D  | grep -v '[0-9]')
for device in $(echo $devlist)
do
  long_name=$(grep ${device##*/}$ testfile | awk '{print $8}')
  echo "${device} ${long_name%%_[0-9]*}"
done
Hmm...I tried running the script but I got error messages:
Code:
debian:~# ./get_usb.sh 
grep: testfile: No such file or directory
/dev/sdd 
grep: testfile: No such file or directory
/dev/sdc 
grep: testfile: No such file or directory
/dev/sdb 
debian:~#
What is testfile? Looks like it suddenly came from nowhere.

Thanks
 
Old 01-30-2009, 05:23 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Uh, sorry. Just tested on my system copying the output from your command. Forgive me. Here is the correct version:
Code:
#!/bin/bash
devlist=$(find /dev/disk/by-path/ -type l -iname \*usb\*scsi\* -print0 | xargs -0 -iD readlink -f D  | grep -v '[0-9]')
for device in $(echo $devlist)
do
  long_name=$(ls -l /dev/disk/by-id/ | grep usb | grep -v part | grep ${device##*/}$ | awk '{print $8}')
  echo "${device} ${long_name%%_[0-9]*}"
done
 
Old 01-30-2009, 05:31 PM   #5
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by colucix View Post
Uh, sorry. Just tested on my system copying the output from your command. Forgive me. Here is the correct version:
Code:
#!/bin/bash
devlist=$(find /dev/disk/by-path/ -type l -iname \*usb\*scsi\* -print0 | xargs -0 -iD readlink -f D  | grep -v '[0-9]')
for device in $(echo $devlist)
do
  long_name=$(ls -l /dev/disk/by-id/ | grep usb | grep -v part | grep ${device##*/}$ | awk '{print $8}')
  echo "${device} ${long_name%%_[0-9]*}"
done
Wohhhhhaaa dude awesome. Worked like a charm. You are amazing.

Taking your "thanked" count to 83....
 
Old 01-30-2009, 05:34 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by kushalkoolwal View Post
Taking your "thanked" count to 83....
He he... thank you! A little gift for you: your first thanked post!
 
Old 01-30-2009, 05:42 PM   #7
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Hi colucix,

Instead of blindly using your code, I was trying to understand how it works and I think I pretty much got it except the following two marked in red color:
Code:
#!/bin/bash
devlist=$(find /dev/disk/by-path/ -type l -iname \*usb\*scsi\* -print0 | xargs -0 -iD readlink -f D  | grep -v '[0-9]')
for device in $(echo $devlist)
do
  long_name=$(ls -l /dev/disk/by-id/ | grep usb | grep -v part | grep ${device##*/}$ | awk '{print $8}')
  echo "${device} ${long_name%%_[0-9]*}"
done
Thanks
 
Old 01-30-2009, 05:49 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
They are examples of parameter substitution in bash. The first one means "strip the longest part of the pattern that matches the front end of the variable":
Code:
${device##*/}
suppose the $device variable is /dev/sdd. The pattern is */. Hence everything before the last slash (included) is removed from the string (leaving just the sdd part). Regarding the $ sign at the end of the grep pattern, it means "at the end of the line".

The second one means "strip the longest part of the pattern that matches the back end of the variable":
Code:
${long_name%%_[0-9]*}
this strip the last part of the string $long_name from the first underscore that comes immediately before a digit (not a character), that is the part in red:
Code:
usb-SanDisk_uSSD_5000_20052444330A224035EA-0:0
Hope it is clear now.

Reference: http://www.tldp.org/LDP/abs/html/par...stitution.html

Last edited by colucix; 01-30-2009 at 05:52 PM.
 
Old 01-30-2009, 06:14 PM   #9
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Yes it is. Thank you once again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing RIP LinuX on a USB drive - device name /dev/sdc becomes /dev/sda Mleahy Linux - Software 1 07-30-2008 08:57 PM
RHEL3 - removing disks shifts LVM dev names downward? QuantSuff Linux - Server 2 06-04-2007 11:58 AM
Firewire and USB disks both want SCSI /dev/sda Crito Linux - Hardware 7 04-25-2006 11:21 AM
Failed to open device /dev/usb/ttyUSB0: No such device efm Linux - Newbie 2 04-04-2005 08:46 PM
usb device names geomatt Linux - Hardware 2 09-05-2004 01:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration