Shell – How to detect NTFS/exFAT file system type from script

exfatfilesystemsntfsshell-script

User has a (incremental) backup script using rsync, to external device. This was erroring on an SSD he had. Turns out his device was formatted exFAT. That means I need to detect this in the script, as I need to alter the options to rsync (e.g., exFAT cannot handle symbolic links, no owner/group permissions, etc.).

User is running Linux Mint. I run Ubuntu. I can only assume/hope that a solution for my Ubuntu will work for his Mint.

I have looked at:

There are a variety of good suggestions there, but I do not see one which meets my requirements, which are:

  • Must report (parseable) ntfs/exfat explicitly, not just say fuseblk (which it will for both exfat & ntfs, I need to distinguish).
  • Must not require sudo.
  • Must be executable starting from a directory path on the file system (can assume it will be mounted), not just starting from a /dev/....

From the suggestions I have tried:

  • fdisk -l, parted -l, file -sL: require sudo and/or /dev/... block device
  • mount: requires /dev/..., only reports fuseblk
  • df -T, stat -f -c %T: accept directory, but report only fuseblk
  • lsblk -f, blkid: require /dev/... block device

Is there a single, simple command which meets all these criteria? Or, lsblk/blkid seem to report exfat/ntfs correctly, if I need to pass them the /dev how do I get that suitably from the directory path in script?

Best Answer

Thanks to the other posters for replying/suggesting. Here is my full solution.

df -P can be used to obtain device from path, and that can be fed to lsblk --fs to obtain exact file system. So a one-liner is:

fs=$( lsblk --fs --noheadings $( df -P $path | awk 'END{print $1}' ) | awk 'END{print $2}' )

If all you need to know is that the file system is fuseblk --- which covers both ntfs & exfat and turns out in the end to be sufficient for my purposes after all --- this can be determined with the much simpler:

fs=$( stat -f -c '%T' $path )
Related Question