Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-29-2009, 11:33 AM   #1
Registered: May 2009
Posts: 34

Rep: Reputation: 0
How to find usb drive from script

I have a usb drive set up. When I view dmesg I can see where it is set to this example it is sda1 then I can manually mount it.
My question is how can I grep dmesg or some other way so I can store the sda1 in a variable in a script. Note that at this point the drive is not mounted so /etc/mtab and /etc/fstab do not have anything for the drive yet.

scsi2 : SCSI emulation for USB Mass Storage devices
Vendor: Maxtor Model: OneTouch Rev: 0125
Type: Direct-Access ANSI SCSI revision: 02
Attached scsi disk sda at scsi2, channel 0, id 0, lun 0
SCSI device sda: 312581808 512-byte hdwr sectors (160042 MB)
sda: sda1
Old 09-29-2009, 01:03 PM   #2
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,168

Rep: Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890
maybe as a service (a program that runs in the background listening for an event to trigger some process) you can do something like[untested]
while [ 1 ]
 usb1=`dmesg | grep sd.: | awk '{print $2}'`
 usb2=`dmesg | grep sd.: | awk '{print $3}'`
 usb3=`dmesg | grep sd.: | awk '{print $4}'`

 sudo mount /dev/$usb1 /mnt/usb1
 sudo mount /dev/$usb2 /mnt/usb2
 sudo mount /dev/$usb3 /mnt/usb3
sleep 5
i think most newer distros do something like this (maybe its called udev) to do something like this but if this is broken on your system perhaps the above will be a remedy.

it will generate many errors so you should redirect stderr however you want

Last edited by schneidz; 09-29-2009 at 01:04 PM.
Old 09-29-2009, 04:57 PM   #3
Registered: May 2009
Posts: 34

Original Poster
Rep: Reputation: 0
actually you are close your grep produces

so by adding
dmesg | grep sd.: | awk '{print $2}' | grep -v device
gives me sda1. There is no reason to run this as a service since I know there is only one drive plugged in. It will either be sda1(if they are running ide) or sdb1 if they are running sata. So this will do just fine. Thanks for the help.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
cant find usb drive taj Mandriva 5 03-21-2008 10:08 PM
Cannot find USB drive... jim_naisium Slackware 4 01-07-2007 09:24 PM
Where can I find my usb drive? slinkysteve Linux - Newbie 3 06-15-2005 05:41 AM
Cannot find USB Drive Zedd129 Fedora 4 03-07-2005 06:43 PM
Cannot find USB Flash Drive jclieber Fedora 6 01-06-2005 07:27 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:03 PM.

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