Share your knowledge at the LQ Wiki.
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 04-29-2008, 07:09 AM   #1
LQ Newbie
Registered: Apr 2008
Posts: 1

Rep: Reputation: 0
Linux API to find out the number of USB disks connected to the machine and the files


I have to write a Linux Program to write a file to USB disks connected to a machine.

->Find out the number of USB disks connected to the machine and the filesystem/partition corresponding to the USB disks
->Mount all the USB disks
->Write the file

I have followed the following approach:

-> dmesg | grep 'removable disk'

The output will be as follows:

“Attached scsi removable disk sdb at scsi8, channel 0, id 0, lun 0”

From this take out fifth field to get the Device name(sda) of Usb disk

-> From /proc/partitions file get the corresponding filesystem name say "/dev/sdb1"

But, I got to know that “dmesg” output may sometimes have different formats(?) and its better to use any available standard Linux API.

The APIs with Libusb library are used to find out the details of USB disk such as bus-name/device-name , device-id/product-id, manufacturer etc.,
I didnt find any API to find out the Usb disk filesystem.

Is there any API or any other standard way for doing this?

Old 04-29-2008, 07:55 AM   #2
Registered: Mar 2008
Distribution: slackware
Posts: 175

Rep: Reputation: 19
Is this program just for your machine, or is it supposed to be portable? I'm not a programmer so I can't help you with any libraries or functions, but I'm you want to know the filesystem on an unmounted device use either fdisk or sfdisk.

matt@matt-laptop:~$ /sbin/fdisk -l /dev/sda

Disk /dev/sda: 1998 MB, 1998585856 bytes
16 heads, 32 sectors/track, 7624 cylinders
Units = cylinders of 512 * 512 = 262144 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 7624 1951728 e W95 FAT16 (LBA)
This will tell you the partition type which should be the same as the file system format.
Old 04-29-2008, 09:04 AM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,352
Blog Entries: 4

Rep: Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334Reputation: 3334
The general approach would be to execute a suitable command (mount, perhaps?), pipe its output and grep it to extract the meaningful lines. Then pipe that output to the next stage whatever-it-is.

Or perhaps you want to read "/etc/fstab" which gives a list of all the filesystems that could be mounted.

General-purpose scripting languages like Perl, Python and PHP are commonly used for these tasks. "Shell scripting" can be used too, but with these other more-powerful tools at your beck and call I see little reason to do so.


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
Find out the number of files currently opened closet geek Linux - Kernel 5 05-19-2015 08:19 AM
[SOLVED] Find out number of users connected to my sftp server procfs Linux - Newbie 5 01-10-2008 10:47 PM
cannot find usb port of my connected printer kpachopoulos Debian 1 08-10-2006 07:29 AM
Find Serial Number of Machine zael Programming 4 10-27-2003 12:39 PM
how do i find number of cpus in one machine? doublefailure Linux - General 3 03-26-2002 02:52 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:32 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
Open Source Consulting | Domain Registration