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
  • Do you want the mount point or the filesystem type? What would your desired output be? – terdon Sep 13 '15 at 18:02

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.


On a system with the GNU stat command installed (as with pretty well any standard Linux distro), you can get the fs type for a given file without requiring any parsing using the stat command:

stat -f -c %T filename

-f tells stat to provide information about the file-system instead of the file, and -c %T sets the output format to include only the human-readable filesystem type (%T).

So you could use that (in bash) as:

if [[ $(stat -f -c %T filename) == ext4 ]]; then
  # ext4 specific command

man stat will provide more information.


With findmnt (part of util-linux):

findmnt -no fstype -T /path/to/file

When using the option

-T, --target path
if the path is not a mountpoint file or directory, findmnt checks path elements in reverse order to get the mountpoint. The other two options suppress the header line: -n, --noheading and select the column(s) to be listed: -o, --output

df from coreutils has a similar option --output= to print only certain fields, like fstype e.g.:

df --output=fstype /path/to/file

there's no option to remove the header though, so you'll have to pipe the output to e.g. | sed 1d

  • A cute findmnt tool, even with line-drawing TUI output. Thanks! – Incnis Mrsi Sep 14 '15 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.