Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

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

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

add comment

7 Answers 7

up vote 3 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
share|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
1  
This doesn't take in to account things live device mapper partitions. –  Kamil Kisiel Feb 17 '10 at 21:22
add comment

Try:

cat /proc/partitions

or

ls /dev/sd*

This will list all available partitions!

share|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
add comment

Here's a perl version:

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

prints:

/dev/sda /dev/sdc 
share|improve this answer
add comment

This shows mounted partitions as a space-delimited list:

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

You can add other filesystem types.

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

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
share|improve this answer
add comment

I think the best solution is to use only one command, like:

    awk '/[a-z]$/ {print $4}' /proc/partitions

or if you have md or dm devices and want to display only sata (sdx) drives:

    awk '$4 ~ /^sd.$/ { print $4 }' /proc/partitions

Also not so pretty, still one-liner, it uses more commands, but works:

    cat /proc/partitions | awk '{print $4}' | grep sd[a-z] | grep -v [0-9]

Just my 2 cents... Hope it helps!

share|improve this answer
add comment

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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