Finding all partitions with filesystems

6,773

I don't think lsblk and file -s is that ugly, but there is an alternate way.
You can use blkid instead. By default, blkid without any arguments will list the known block devices, and a little bit of information about them, including the filesystem type. The format is also in key=value pair format (by default), which makes it easy to dump into a script.

This is what the output looks like on my system:

>> blkid
/dev/sda1: LABEL="boot" UUID="5F6E-FD2B" TYPE="vfat" 
/dev/sda2: UUID="yBVbC2-MFnP-1T1s-9XGz-VCUH-S5oG-aNSXDg" TYPE="LVM2_member" 
/dev/mapper/sys-root: UUID="0e1e5a6b-31b2-4e13-9b26-cbbb74e95ab9" TYPE="xfs" 
/dev/mapper/sys-var--log: UUID="49f1b45d-d303-4c2e-a72b-c75e8f1e27ae" TYPE="xfs" 
/dev/mapper/sys-usr--portage: UUID="b8a494dd-f7f4-4e5e-9975-e21a61c95d22" TYPE="xfs" 
/dev/mapper/sys-stmp: UUID="f2b3252c-3ec2-4c66-bed2-26c93f12b535" TYPE="xfs" 
/dev/mapper/sys-home--phemmer--luks: UUID="898f9f52-3c9d-475c-9e7b-1a2263778a39" TYPE="crypto_LUKS" 
/dev/mapper/_dev_dm_4: UUID="d0cb5255-e35a-4ee2-94ca-f1a7f7339eb9" TYPE="xfs" 
Share:
6,773
Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I'm writing a script that will copy all of the files on a device to a directory. The problem is that some of the devices have multiple partitions and some of the partitions don't have filesystems to copy files from. At the moment, I'm thinking about using lsblk to get a list of partitions on the device and file -s to check for a filesystem on each partition.

    Is there a less brute way to do what I am trying to do?


    Here is information regarding the empty partition problem:

    # /dev/sdb is a flashdrive with two partitions
    # /dev/sdb1 has no filesystem
    # /dev/sdb2 has an ext4 partition
    $ lsblk -fi
    ...
    sdb    vfat   CARRIER-R C84B-6A72                            
    |-sdb1 vfat   CARRIER-R C84B-6A72                            
    `-sdb2 ext4   CARRIER-R 33ebb632-68a5-4bf5-bd29-90733af9699e
    ...
    
    $ lsblk -ln -o NAME,FSTYPE
    ...
    sdb  vfat
    sdb1 vfat
    sdb2 ext4
    ...
    
    # As confirmation, mounting the partition fails
    $ mount -t auto /dev/sdb1 /mnt
    mount: wrong fs type, bad option, bad superblock on /dev/sdb1
    ...
    
    $ dmesg | tail
    ...
    [  985.933627] EXT4-fs (sdb1): VFS: Can't find ext4 filesystem
    [  985.935722] EXT4-fs (sdb1): VFS: Can't find ext4 filesystem
    [  985.937603] EXT4-fs (sdb1): VFS: Can't find ext4 filesystem
    [  985.939623] FAT-fs (sdb1): invalid media value (0xa7)
    [  985.939627] FAT-fs (sdb1): Can't find a valid FAT filesystem
    
    • derobert
      derobert about 11 years
      udisks is probably your best bet, if you have it running.
    • Stéphane Chazelas
      Stéphane Chazelas about 11 years
      See the -f option of lsblk.
    • Admin
      Admin about 11 years
      @don_crissti: Thanks. I was trying lsblk -fnr | grep '^sda.\ ' | awk '{ print "$1 $2" }' before. I should have spent more time in the man page.
    • Admin
      Admin about 11 years
      I seem to be having one problem. lsblk seems to think that partitions without filesystems on them have vfat filesystems.
    • Admin
      Admin about 11 years
      I'll try a different flashdrive. Just in case it's relevent, I am using Arch Linux with the stock kernel. The tools were installed from their respective pacman packages.
    • Admin
      Admin about 11 years
      I have narrowed down the problem a bit. The flashdrive had a GPT partition table. If I make an MBR partition table on it, lsblk doesn't show filesystem information for partitions without filesystems.
    • Admin
      Admin about 11 years
      I zeroed out all of the partitions on the drive and lsblk no longer detects filesystems on them. There must have been some lingering filesystem data lying around. I should have realized that earlier. I am curious as to why lsblk detected filesystems on the filesystem-less partitions while blkid did not.
  • Xanderale
    Xanderale about 11 years
    I'm going to go ahead and use blkid: blkid -o full | grep 'TYPE=' | grep -o '^/dev/sdb.'
  • Unrealist
    Unrealist over 9 years
    The problem that I have faced so far with blkid is that it does not print all the devices until I have run it with sudo at least once. That is to say, on the later invokes of blkid it prints out all the devices fine, only the first time it needs sudo. On the other hand, since I don't care about dirty if I can get what I want, I think udisksctl (or in other systems udisks is really a good tool since it gives out all the info without being sudo invoked and I can use grep and awk to get my fields easily. I like to keep sudo out of my scripts as much as possible.