Find filesystem of an unmounted partition from a script

I think I found the answer: blkid

From the man page:

The blkid program is the command-line interface to working with the libblkid(3) library. It can determine the type of content (e.g. filesystem or swap) that a block device holds, and also attributes (tokens, NAME=value pairs) from the content metadata (e.g. LABEL or UUID fields).

Apparently it prints the device name along with the filesystem type (along with some other useful information). To get a list of all devices with their types:

blkid | sed 's!\(.*\):.*TYPE="\(.*\)".*!\1: \2!'

To find all /dev/sd*/ devices, just add in a grep:

blkid | grep "/dev/sd.*" | sed 's!\(.*\):.*TYPE="\(.*\)".*!\1: \2!'

Then just cut or awk to get what you need.


file -s /path/to/device will identify the filesystem on a disk/partition/slice.

A la:

[root@node2 ~]# file -s /dev/mapper/VolGroup00-LogVol00
/dev/mapper/VolGroup00-LogVol00: Linux rev 1.0 ext3 filesystem data (needs journa recovery) (large files)
[root@node2 ~]# file -s /dev/mapper/coraid--pool-coraid--lv1 
/dev/mapper/coraid--pool-coraid--lv1: Linux GFS2 Filesystem (blocksize 4096, lockproto lock_dlm)
[root@node2 ~]# file -s /dev/mapper/coraid--pool-coraid--lv2 
/dev/mapper/coraid--pool-coraid--lv2: SGI XFS filesystem data (blksz 4096, inosz 256, v2 dirs)

at least, on Linux it does.


Show only the block device's TYPE tag, and only output its value:

blkid -s TYPE -o value "$device"

Example:

$ blkid -s TYPE -o value /dev/mapper/vg_svelte-home 
btrfs