Shell commands to determine if file is held on an APFS volume

In a shell script, I need to determine if a file is held on an AFPS volume. What is the easiest way to do that?


Solution 1:

I'd do the following:

  1. Use a combination of basename (to get the full path of the file, if required) and df to determine which volume the file is on.
  2. Use diskutil info to determine the filesystem type of the volume (you could also use mount).

There are perhaps better ways of doing it but that will certainly work.

The following one-liner was suggested by fd0 in a comment:

df -T apfs /absolute/path/to/file >/dev/null && Do Stuff

This executes "Do Stuff" only if the file is on an APFS filesystem. If Do Stuff is more than a few simple commands the same can be accomplished with

if df -T apfs /absolute/path/to/file >/dev/null; then
    do stuff
    do even more stuff
fi