By default, when removable media is inserted Ubuntu will open Nautilus in the mount directory. I disabled this feature, but was wondering if I could configure Gnome to open a terminal in the mount directory instead.
EDIT: I am using Ubuntu 15.10.
By default, when removable media is inserted Ubuntu will open Nautilus in the mount directory. I disabled this feature, but was wondering if I could configure Gnome to open a terminal in the mount directory instead.
EDIT: I am using Ubuntu 15.10.
An edited version of this script does the job.
When a (any) usb device is being connected, a gnome-terminal
is opened in its (root) directory.
In the example, when a 14.04 usb
startup flash drive is connected:
#!/usr/bin/env python3
import os
import subprocess
import time
def get_mountedlist():
return [(item.split()[0].replace("├─", "").replace("└─", ""),
item[item.find("/"):]) for item in subprocess.check_output(
["lsblk"]).decode("utf-8").split("\n") if "/" in item]
def identify(disk):
command = "find /dev/disk -ls | grep /"+disk
output = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
if "usb" in output:
return True
else:
return False
done = []
while True:
mounted = get_mountedlist()
new_paths = [dev for dev in mounted if not dev in done and not dev[1] == "/"]
valid = [dev for dev in new_paths if identify(dev[0]) == True]
for item in valid:
os.chdir(item[1])
subprocess.Popen(["gnome-terminal"])
done = mounted
time.sleep(4)
open_usb.py
Test- run the script. with the command:
python3 /path/to/open_usb.py
If all works fine, add it to Startup Applications: Dash > Startup Applications > Add the command:
python3 /path/to/open_usb.py
python3
)As mentioned in a comment, although the script works as it should, one issue occurs when you safely remove the usb
device: a warning is given that the volume is "occupied" by the script.
The cause is that the script cd
-s into the directory of the volume, before opening the terminal in the root directory of the volume.
The solution is quite simple; make the script leave the directory again after having opened the terminal in the usb
device's root directory. In the version below, the issue is fixed:
#!/usr/bin/env python3
import os
import subprocess
import time
home = os.environ["HOME"]
def get_mountedlist():
return [(item.split()[0].replace("├─", "").replace("└─", ""),
item[item.find("/"):]) for item in subprocess.check_output(
["lsblk"]).decode("utf-8").split("\n") if "/" in item]
def identify(disk):
command = "find /dev/disk -ls | grep /"+disk
output = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
if "usb" in output:
return True
else:
return False
done = []
while True:
mounted = get_mountedlist()
new_paths = [dev for dev in mounted if not dev in done and not dev[1] == "/"]
valid = [dev for dev in new_paths if identify(dev[0]) == True]
for item in valid:
os.chdir(item[1])
subprocess.call(["gnome-terminal"])
os.chdir(home)
done = mounted
time.sleep(4)