how to check the format of a filesystem Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of how to check the format of a filesystem without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I would like to check, in a bash script, on what filesystem type a directory is.

The idea is something like

if [path] is on a [filesystem] filesystem then
   filesystem specific command
end if


Use df. You can pass it a path, and it will give you the filesystem information for that path. If you need the filesystem type, use the -T switch, like so:

$ df -T test
Filesystem     Type 1K-blocks     Used Available Use% Mounted on
/dev/sda2      ext4 182634676 32337180 141020160  19% /home

To extract the filesystem type, you can parse it (use the -P switch to avoid df breaking lines if the device part is too long):

$ df -PT test | awk 'NR==2 {print $2}'

So you can use that value in an if construct like so:

if [ "$(df -PT "$path" | awk 'NR==2 {print $2}')" = "ext4" ] ; then
  it is an ext4 filesystem

Beware that the device column can contain spaces (but it’s rare), in which case the parsing will fail.

We are here to answer your question about how to check the format of a filesystem - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji