简体   繁体   中英

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 ?

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM