James Dean
James Dean

Reputation: 165

Trying to get recent External Hard Drive name (with spaces) via terminal

I've been using this command to retrieve recent hard drive connected to mac:

mount | tail -1 | cut -d' ' -f3

Problem is that if a drive has spaces, then it only grabs the first word of that external harddrive. How can I get the whole name of the drive, whether it has one word or spaces with many words?

Upvotes: 0

Views: 369

Answers (2)

Ali ISSA
Ali ISSA

Reputation: 408

mount | tail -1 | sed -e "s/^[^/]*\(.*\) type .*$/\1/g"

Upvotes: 0

isedev
isedev

Reputation: 19641

The output of mount (OSX) is:

<device_path> on <mount_point> (<type>,<options_list>)

So the alternative approach is to extract everything before the word on excluding the preceeding space (or to remove the space, the word on and everything else to the end of line):

mount | tail -1 | sed 's/ on .* ([^)]*)$//'

Although, based on your command, you seem to be interested in the mount point, not the device name. In that case, it becomes "extract the characters between on and type excluding leading and trailing spaces:

mount | tail -1 | sed '/^.* on \(.*\) ([^)]*)$/\1/'

The ([^)]*) component avoids matching problems in corner cases (where the device name or mountpoint contains space, on, space).

Upvotes: 1

Related Questions