Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
udisks is probably your best bet, if you have it running. – derobert May 1 '13 at 17:47
See the -f option of lsblk. – Stéphane Chazelas May 1 '13 at 18:51
@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. – Evan Teitelman May 1 '13 at 19:04
I seem to be having one problem. lsblk seems to think that partitions without filesystems on them have vfat filesystems. – Evan Teitelman May 1 '13 at 19:05
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. – Evan Teitelman May 1 '13 at 19:57
up vote 6 down vote accepted

I dont 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|improve this answer
I'm going to go ahead and use blkid: blkid -o full | grep 'TYPE=' | grep -o '^/dev/sdb.' – Evan Teitelman May 1 '13 at 20:31
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. – Unrealist Feb 11 '15 at 0:56

Your Answer


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.