Results 1 to 6 of 6

Thread: How do I Get a Device's UUID in Shell Script?

  1. #1
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question How do I Get a Device's UUID in Shell Script?

    How do I automatically get the UUID of a device from the device name? I've been trying to get it through blkid and sed, but this is as far as I've got:
    Code:
    $ blkid /dev/sdb1 | sed 's_[^#]*UUID="\([-[:alnum:]]*\)"_\1_'
    B6BF-00BD TYPE="vfat"
    Any help would be greatly appreciated. More info here.


    Solution:
    Code:
    blkid -o value -s UUID /dev/sdb1
    You can also replace UUID with LABEL to get the label of a device.
    Last edited by Penguin Guy; May 3rd, 2010 at 03:14 PM.

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How do I Get a Device's UUID in Shell Script?

    Quote Originally Posted by Penguin Guy View Post
    How do I automatically get the UUID of a device from the device name? I've been trying to get it through blkid and sed, but this is as far as I've got:
    Code:
    $ blkid /dev/sdb1 | sed 's_[^#]*UUID="\([-[:alnum:]]*\)"_\1_'
    B6BF-00BD TYPE="vfat"
    Any help would be greatly appreciated. More info here.
    Code:
    sudo blkid /dev/sdb1 | perl -pe 's/.*UUID="(.*?)".*/$1/'
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: How do I Get a Device's UUID in Shell Script?

    Or

    Code:
    sudo blkid /dev/sdb1 | cut -d '"' -f 2
    「明後日の夕方には帰ってるからね。」


  4. #4
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do I Get a Device's UUID in Shell Script?

    Quote Originally Posted by Bachstelze View Post
    Or

    Code:
    sudo blkid /dev/sdb1 | cut -d '"' -f 2
    I'm afraid that doesn't work for all devices, since the list of properties is not always in the same order.

    Quote Originally Posted by slavik View Post
    Code:
    sudo blkid /dev/sdb1 | perl -pe 's/.*UUID="(.*?)".*/$1/'
    Thanks, that works great!


  5. #5
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Arrow Instructions in fstab

    Hey, looks like I found a better solution in fstab itself:
    Code:
    blkid -o value -s UUID /dev/sdb1
    Sorry for bothering you.

  6. #6
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Instructions in fstab

    Quote Originally Posted by Penguin Guy View Post
    Hey, looks like I found a better solution in fstab itself:
    Code:
    blkid -o value -s UUID /dev/sdb1
    Sorry for bothering you.
    hi
    i propose, if you are running blkid in a script, to use the "-c" option. this causes the command to read
    from a clean cache ( not /etc/blkid.tab --> /dev/.blkid.tab - those files may be previously but no more
    available ( e.g. removeable ).
    use: blkid -c /dev/null /dev/xyzn

    ciao
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •