-
Notifications
You must be signed in to change notification settings - Fork 12
/
plug
executable file
·47 lines (39 loc) · 1.79 KB
/
plug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# Description
# ----------
# Interactive (un)mount of hotplug devices (USB drives) from console using FZF!
# Outputs path to mounted device so could be used in alias to actomatically
# `cd` to mounted directory.
# Author: [Dmitry](http://dmi3.net) [Source](https://github.com/dmi3/bin)
# Requirements
# ----------
# 1. `sudo apt-get install jq fzf udisks2`
# 2. `alias unplug='plug -u'`
# 3. `cd` to mounted directory:
# - Fish shell: `alias plug='cd (command plug)'`
# - Bash shell: `alias plug='cd $(command plug)'`
# Usage
# -----
# $ plug
# > ACRONIS_MED DataTraveler_2.0 sdb1
# DATA DataTraveler_3.0 sdc1
# Mounted /dev/sdb1 at /media/dmi3/ACRONIS_MED.
#
# $ plug -u
# > /media/dmi3/ACRONIS_MED ACRONIS_MED DataTraveler_2.0 sdb1
# / workbuntu2020 Samsung_SSD_860_EVO_M.2_500GB sda2
# /media/dmi3/ElTorito ElTorito Samsung_SSD_860_EVO_M.2_500GB sda1
# Unmounted /dev/sdb1
if [[ $1 == "-u" ]]; then
filter="!="
else
filter="=="
fi
DEVICE=$(lsblk -J -o HOTPLUG,NAME,LABEL,MODEL,MOUNTPOINT | jq -r ".blockdevices | .[] | . as \$p | .children | flatten | .[] | select( .hotplug == true and .mountpoint $filter null ) | [.mountpoint,.label,\$p.model,.name] | @tsv" | fzf -d$'\t' | cut -d$'\t' -f4)
if [[ $1 == "-u" ]]; then
sync
udisksctl unmount --force -b "/dev/$DEVICE"
else
udisksctl mount -b "/dev/$DEVICE" > /dev/null
echo $(lsblk -o MOUNTPOINT -n "/dev/$DEVICE")
fi