If i run fdisk -l I get an output as:

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0003ad9d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        1306    10490413+  83  Linux
/dev/sda2            1307        2612    10490445   83  Linux

Disk /dev/sdb: 120.0 GB, 120034123776 bytes
        255 heads, 63 sectors/track, 14593 cylinders
        Units = cylinders of 16065 * 512 = 8225280 bytes
        Disk identifier: 0x0003ad9d

       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        1306    10490413+  83  Linux
    /dev/sdb2            1307        2612    10490445   83  Linux

How can I just show the disks available on system such that they may with a space between them as:

/dev/sda /dev/sdb /dev/sdc

link|improve this question

feedback

migrated from stackoverflow.com Feb 15 '10 at 16:14

This question came from our site for professional and enthusiast programmers.

6 Answers

up vote 4 down vote accepted

You can use pipes to redirect the output

 fdisk -l | grep ^Disk | awk -F:  '{ print $1 }' |  awk -F" "  '{ print $2 }'
  • fisk -l: get the full disk output
  • grep Disk: filter the line starting with Disk
  • awk -F: '{ print $1 }': get first part where the separator is ":"
  • awk -F" " '{ print $2 }': get the second part where the separator is space
link|improve this answer
1  
In some blatant shell golf, I came up with this: fdisk -l 2>/dev/null | egrep -o '(/dev/[^:]*):' |awk -F: '{print $1}' – Bill Weiss Feb 15 '10 at 16:43
2  
fdisk -l 2>/dev/null | egrep -o '(/dev/[^:]*):' | egrep -o '([^:]*)' One better :) – Bill Weiss Feb 15 '10 at 16:46
2  
cat /proc/partitions | egrep -o '.d.' | sort | uniq works on my machine, but I'm not sure how hacky it is. Probably fails on machines with more than sda-sdz :) – Bill Weiss Feb 15 '10 at 16:48
This doesn't take in to account things live device mapper partitions. – Kamil Kisiel Feb 17 '10 at 21:22
feedback

Here's a perl version:

fdisk -l | perl -n -e 'if (m{^Disk (\/.*):})  { print $1," "} END { print "\n"}'

prints:

/dev/sda /dev/sdc 
link|improve this answer
feedback

Try:

cat /proc/partitions

or

ls /dev/sd*

This will list all available partitions!

link|improve this answer
1  
I wish I could give half an upvote. ;) 'cat /proc/partitions' works fine, 'ls /dev/sd*' will sometimes work, but won;t necessarily list all drives. so, for example: <pre> root>cat /proc/partitions major minor #blocks name 202 0 5242880 xvda 202 16 262144 xvdb 202 32 5242880 xvdc root>ls /dev/sd* ls: cannot access /dev/sd*: No such file or directory </pre> – Don Branson Feb 15 '10 at 20:44
@Don: Use backticks instead of <pre> in comments. – Dennis Williamson Feb 16 '10 at 1:57
feedback

This shows mounted partitions as a space-delimited list:

echo $(mount -t ext2,ext3 | cut -f1 -d' ')

You can add other filesystem types.

link|improve this answer
It will shows only the mounted devices, what about the another ones? – Cesar Romero Feb 15 '10 at 23:58
feedback

Here's a few other alternatives, if you can make certain assumptions (I like /proc/partitions, though, as long as you're filtering on the major numbers so you don't pick up LVM volumes, etc)

Assuming your disks all are local (ie, not SAN):

user@host [/home/q0px]
$ ls -l /dev/disk/by-path/
total 0
lrwxrwxrwx 1 root root 9 Dec  5 01:27 pci-0000:00:1f.1-ide-0:0 -> ../../hda

If your partitions all have UUIDs (works for SAN devices, too)

user@host [/home/q0px]
$ ls -l /dev/disk/by-uuid 
total 0
lrwxrwxrwx 1 root root 18 Dec  5 01:27 dc13d5cf-ba98-4339-99c5-0136177f7e22 -> ../../cciss/c0d0p1

If your partitiona all have labels (also orks for SAN devices):

user@host [/home/q0px]
$ ls -l /dev/disk/by-label/
total 0
lrwxrwxrwx 1 root root 18 Dec  5 01:27 boot -> ../../cciss/c0d0p1
link|improve this answer
feedback

i guess commands like the one u used, or df(which lists the devices while showing the free and used space) will not output the data in space delimited fashion as u want. U need to use a string processing command like sed 'piped' to the output of previous disk commands and get the desired oputput.

-AD.

link|improve this answer
2  
Uv xceded teh tweet len lim. – Dennis Williamson Feb 15 '10 at 20:24
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.