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.

6
  • Have you checked out this link from what appears to be our user @Seth perhaps? – KGIII Nov 5 '15 at 2:07
  • @Seth None of the options seem applicable, ie I don't see an option for USB or external hard drive. – Alessandro Power Nov 5 '15 at 2:16
  • You don't have a "removable media" option? (USB drive is removable media.) – KGIII Nov 5 '15 at 2:19
  • @KGIII Under the removable media tab, there are options for CD Audio, DVD video, etc., but no USB. – Alessandro Power Nov 5 '15 at 2:30
  • Wow... I'm gonna need a minute. I think I have it. I'll type out an answer and give you a screenshot in a few - it will be a minute. – KGIII Nov 5 '15 at 3:00
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)
6
  • @AlessandroPower Glad it works :) – Jacob Vlijm Nov 5 '15 at 14:23
  • Actually I have one last issue. When I try to safely remove a USB I am warned that open_usb.py is keeping the device busy. Do you know how this can be prevented? – Alessandro Power Nov 7 '15 at 15:43
  • @AlessandroPower I assume the terminal is still open then? (Since that is the only connection after the command has run) – Jacob Vlijm Nov 7 '15 at 15:51
  • No, I closed the terminal. – Alessandro Power Nov 7 '15 at 16:06
  • @AlessandroPower I'll look at it tonight (have some visitors :) ) – Jacob Vlijm Nov 7 '15 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.