9

How could I check the file system format of a disk image?

I know that I can check with the file command but I would like to automate the behaviour.

$ file img.raw
img.raw:   data

$ file img.ext4
img.raw:   Linux rev 1.0 ext4 filesystem data, UUID=346712e7-1a56-442b-a5bb-90ba4c6cc663 (extents) (64bit) (large files) (huge files)

$ file img.vfat
img.vfat:  DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 16, reserved sectors 16, root entries 512, Media descriptor 0xf8, sectors/FAT 256, sectors/track 32, heads 64, sectors 1024000 (volumes > 32 MB) , serial number 0x4b5e9a12, unlabeled, FAT (16 bit)

I would like to check if the given image disk is formatted with the given format.

For example checkfs <image> <format> returns 0 if the image contains a 'format' file system, another value otherwise.

I thought about doing something like file <image> | grep <format> and check the return code, however for vfat filesystems, 'vfat' is not appearing on file's output.

I could also write a script to do it but I can't find tools which allow me to get the file system format of a disk image.

I've also tried with the following tools: fdisk, parted, and df.

Is there a tool which would allow me to check a disk image file system format and that works with most used file system formats?

3
  • So what was wrong with parted? Commented Apr 17, 2018 at 19:44
  • There is no option to get file system type with parted. So I have to print the full partition table and then parse output using sed or awk. I think there are better solutions Commented Apr 18, 2018 at 8:05
  • 1
    It has a machine parsable output mode if you wanted to do that. Commented Apr 23, 2018 at 17:49

1 Answer 1

11

Finally found what I needed

  • blkid -o value -s TYPE <image> will return the fs type or nothing if it's raw data.

EDIT:

As mentioned by @psusi, parted has a machine parsable output. I find it less convenient than using blkid but it could also be useful.

  • parted -m <image> print | tail -n +3 | awk -F ":" '{print $(NF-2)}' will print the fs type of each partition.

tail -n +3 is used to skip the two first lines
awk -F ":" '{print $(NF-2)}' is used to get the fs type which is the third last element starting from the end

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.