2

I want to write a shell script, which will run fdisk -l and extract a string /dev/sdbn from the output of fdisk -l.

Using extracted word I want to mount it to /mnt.

For Example:

Disk identifier: 0x00000000

Device     Boot    Start    End    Blocks    Id    System
/dev/sdb1    *         1    493    3956672    6     FAT16

How do I only extract the string /dev/sdb1?

2
  • 1
    How do you determine what device you are trying to filter on? Do you want everything that is /dev/sdbX`? It's not clear what you are actually asking for...
    – jasonwryan
    Commented Jul 7, 2014 at 9:25
  • Are you sure that this is really what you need? With the device plugged in, you can find out what partition it has by looking inside /dev, e.g. if you know it's sdb you can list /dev/sdb[1-9]*. Commented Jul 7, 2014 at 21:54

5 Answers 5

3

Try:

fdisk -l | grep -o '^/dev/sdb[0-9]'

The -o option causes grep to print only matching pattern.

Updated

If you want all except /dev/sdaX, you can use:

fdisk -l | grep -o '^/dev/sd[b-z][0-9]'
5
  • Thank You so much...this is what I exactly wanted. @Gnouc
    – Rahul
    Commented Jul 7, 2014 at 9:30
  • suppose when I have another device mounted simultaneously, at that time this will not work as you described. For example, when I mount two device at a time, then how do I extract both string i.e /dev/sdb1 and /dev/sdc1 ?
    – Rahul
    Commented Jul 7, 2014 at 9:36
  • awk and sed commands are much faster than the grep
    – Thushi
    Commented Jul 7, 2014 at 13:34
  • @Thushi: really? Can you give some evidences?
    – cuonglm
    Commented Jul 7, 2014 at 15:13
  • @Thushi that is not true. While you might be able to find specific cases where sed or awk are faster than the equivalent grep, it is not a general rule. In most cases, grep will be faster than either sed or awk. Even in this very simple case, the awk approach took 0.02968 seconds while the grep took 0.02782. This is the average over 100 runs. Please don't make unsubstantiated claims like that.
    – terdon
    Commented Jul 7, 2014 at 15:50
2

Use this command : fdisk -l | awk '/\/dev\/sdb[0-9]/ {print $1}'

5
  • That's just an example to get the first word from the line.He can filter out the lines using his logic.
    – Thushi
    Commented Jul 7, 2014 at 9:25
  • @jasonwryan: how about this???
    – Thushi
    Commented Jul 7, 2014 at 9:34
  • That's much better! You could throw in an anchor, ^, for good measure... Upvoted.
    – jasonwryan
    Commented Jul 7, 2014 at 9:35
  • suppose when I have another device mounted simultaneously, at that time this will not work as you described. For example, when I mount two device at a time, then how do I extract both string i.e /dev/sdb1 and /dev/sdc1 ?
    – Rahul
    Commented Jul 7, 2014 at 9:41
  • @arzyfex:Then the command will be: fdisk -l | awk '/\/dev\/sd[a-z][0-9]/ {print $1}'
    – Thushi
    Commented Jul 7, 2014 at 9:43
0

You can use sed...

Since a directory-path contains /, I've chosen to use _ to delimit the sed-command (although one usually use /, any symbol can be used as delimiter).

fdisk -l | sed -n 's_^\(/dev/[^ ]*\) .*$_\1_p'

2
  • that works but it gave me all partition like, /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4 /dev/sda5 /dev/sda6 /dev/sda7 /dev/sda8 /dev/sdb1 rather than this, I just want /dev/sdb1, any help on this ?
    – Rahul
    Commented Jul 7, 2014 at 9:19
  • Oh, my bad... I misunderstood. You could use head -n 1 after the sed - or simply fgrep 'sdb1' or fgrep '/dev/sdb1' before or after the sed... You can also simply change the script and replace /dev/[^ ]* with /dev/sdb1 and keep the rest as before. Commented Jul 7, 2014 at 9:30
0

Try this for getting /dev/sdb1

fdisk -l | grep -w "/dev/sdb1" | awk '{print $1}'
1
  • I guess grep is not required.awk only can match the pattern and print the first word fdisk -l | awk '/\/dev\/sdb1/ {print $1}'
    – Thushi
    Commented Jul 7, 2014 at 12:26
0

For those who don't have root access, you can use a simple ls:

ls /dev/sd* | grep -o '/dev/sd[a-z][1-9]'

Edit regular expression as you want.

UPDATE: As @StéphaneChazelas mentioned, presence of device files won't confirm existence of the underlying device. It would be safer to query /proc/partitions instead:

awk '$NF ~ /^sd[a-z][0-9]+/ {print "/dev/" $NF}' < /proc/partitions
1
  • Or just echo /dev/sd[a-z][1-9], though depending on your system those devices files may exist even though the underlying device doesn't exist. Also awk '$NF ~ /^sd[a-z][0-9]+/ {print "/dev/" $NF}' < /proc/partitions to query what the kernel really knows about partitions on devices using the sd subsystem. Commented Oct 11, 2015 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .