0

In a shell script, I need to determine the device for a file, subsequent use with diskutil, eg...

# ??? Assign device to FileDevice, based on full path.
diskutil info $FileDevice
3
  • How does this relate to your other question?
    – nohillside
    Commented Mar 23, 2020 at 15:58
  • My functional need changed a little since that one; now it's to determine the type of volume, not just if it's APFS or not. Commented Mar 23, 2020 at 17:56
  • 1
    @nohillside Here's my combined solution: Returns file system personality for a file: diskutil info $(df $filepath | sed -n 2p | cut -d ' ' -f 1) | grep '^ File System Personality:*' | sed 's/ File System Personality: (.*)$/\1/' Commented Mar 23, 2020 at 18:05

2 Answers 2

2
df /path/to/file | sed -n 2p | cut -d ' ' -f 1

will give you the device the file is on. To combine this directly with diskutil info use

diskutil info $(df /path/to/file | sed -n 2p | cut -d ' ' -f 1)
1

The easiest way to determine the device that contains a file is to use df. For example:

$ df /Users/mj/bin/imgls
Filesystem   1K-blocks      Used Available Capacity iused               ifree %iused  Mounted on
/dev/disk1s1 488347692 418217076  38565452    92% 3141515 9223372036851634292    0%   /

You can use cut if you just want the device (I used tail as a crude way to remove the df header).

$ df /Users/mj/bin/imgls | tail -1  | cut -d ' ' -f 1
/dev/disk1s1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .