2

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.

Alessandro Power
  • 333
  • 1
  • 6
  • 17

1 Answers1

1

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:

enter image description here

The script

#!/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)

How to use

  • Copy the script into an empty file, save it as 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
    

Notes

  • I tested it on Unity (14.04), but it is unlikely it would break on any Ubuntu version, as long as it has the default configuration (including python3)
  • The script only runs a very simple and lightweight check, every four seconds. In my tests, I could not determine any additional processor load.



EDIT

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

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)
Jacob Vlijm
  • 83,767