14

I created a shortcut via "make link" option. When I enter to this shortcut's folder I can't see any folders above it so I can't easily navigate to them.

Is there a way to go one folder up in the GUI? Maybe a hotkey? (Can't do cd .. this time ^__^ ).

enter image description here


In Windows for example I am indeed able to navigate in the way I describe, here is a Win10 image that explains it:

enter image description here

  • Have you tried the Backspace key? – PenguinCSC Jan 15 '16 at 20:27
  • In nautilus less than 3.16 it's easy to add a parent button to nautilus toolbar though has to be done via the source > rebuild (patch or a ppa build for 14.04 available) Otherwise try Alt+up key – doug Jan 16 '16 at 12:43
  • Alt+Up brings me from the link's destination to the desktop... So sadly no :\ If someone here have the right connections he might offer this directly to the dev team? I've very new so I can't really offer it directly, without the data have to pass through many people (I guess)... – JohnDoea Jan 16 '16 at 12:47
  • Ah - I see what you mean. Maybe try Jacob's solution or create links yourself. Your current links are seen as a directory so up is the dir. the link is in, typically the Desktop. If you create links using an executable .desktop file then navigation after using that 'link' will be normal. – doug Jan 16 '16 at 13:25
  • Same question here askubuntu.com/questions/713397/show-full-path-for-links (if solved, to mark other as duplicate) – user.dz Jan 16 '16 at 14:10
17
+100

Why this is a challenging question

The question has a few challenges:

  • nautilus does not communicate directly from the command line, to get the currently active directory for example, nor can you "send" the currently opened folder (-window) to another directory from the command line.
  • In the current path, as requested via "NAUTILUS_SCRIPT_CURRENT_URI", Nautilus does not return the real path to the current folder, but "sees" the link as if it was an actual folder.

The solution therefore is as dirty as it gets; we need to find workarounds. Below four options to solve the issue.

1. right- click to go one level up

To get the real path to the current directory, we have to retrieve information from the link. We can do that either by using ls -l on the link, which will output e.g.:

lrwxrwxrwx 1 jacob jacob 35 jan 15 08:23 /home/jacob/Bureaublad/Flyer_st/Verwijzing naar Site_current -> /home/jacob/Bureaublad/Site_current

where the section after -> is the real path inside the symlink, or, using python:

real = os.path.realpath("/path")

Using this in a nautilus script, we can indirectly get the real path to the current file or folder.

Then if we have the path, how do we make nautilus move one level up?

Again, we cannot solve this and keep our hands clean. To move one level up, we first edit the found path a bit, from:

/path/to/a/folder

into

/path/to/a

then, using xdotool to simulate Ctrl+L (the GUI shortcut to insert a path into a nautilus window, since there is no cli option to move to another directory using the current window), and subsequently make xclip paste the edited path + Enter, we have a working solution!

In practice

  1. We are in a folder, opened from a link ("Link to Telegram") on my Desktop. The real folder is a sub folder of my Downloads folder:

    enter image description here

  2. Then if we right- click on any of the files inside the folder to run the script:

    enter image description here

  3. Automatically, the path to the superior directory is inserted:

    enter image description here

  4. And, also automatically, Return is pressed, and we move one directory level up:

    enter image description here

The script

#!/usr/bin/env python3
import subprocess
import os
import time

def run(cmd):
    subprocess.call(["/bin/bash", "-c", cmd])

current = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI").replace("file://", "").replace("%20", " ")
real = os.path.realpath(current)
up = os.path.abspath(os.path.join(real, os.pardir))
run("xdotool key Control_L+l")
run("printf '"+up+"' | xclip -selection clipboard")
run("xdotool key Control_L+v")
# confirm by virtually press Return
time.sleep(0.1)
run("xdotool key Return")

How to set up

  1. The script needs both xdotool and xclip:

    sudo apt-get install xdotool xclip
    
  2. create, if it doesn't exist already, the directory

    ~/.local/share/nautilus/scripts
    
  3. Copy the script above into an empty file, save it as level_up (no extension) in ~/.local/share/nautilus/scripts, and make it executable

  4. You might have to log out and back in.
  5. Now you should be able to run the script by right- click on a file (any) > scripts > level_up:

    enter image description here


[EDIT] I changed the script above to paste the path into the nautilus window, instead of making xdotool type it. It needs xclip to be installed, but it is a major improvement, especially on very long paths.


2. Alternatively, open a new nautilus window in the superior directory

You could avoid using xdotool, by making the script open a new nautilus window, in the parent's directory. The script would then be even shorter:

#!/usr/bin/env python3
import subprocess
import os

def run(cmd):
    subprocess.call(cmd)

current = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI").replace("file://", "").replace("%20", " ")
real = os.path.realpath(current)
up = real[:real.rfind("/")]
subprocess.Popen(["nautilus", up])

In this case, you wouldn't need to install xdotool. We could even extend the script by closing the original window and placing the new window exactly in the same position (& size).

The downside is that the history of the original window is lost this way.

3. An additional solution: alternative way to (automatically) create links

Not relevant to existing links, but when used from the GUI, a nautilus script to automatically create executable .desktop files on right-click might be helpful:

right-click on the directory to create a shortcut (behaving as a link)

enter image description here

Unlike symlinks, these links will take you to the actual folder, without behaving as a folder itself:

enter image description here

The script

#!/usr/bin/env python3
import subprocess
import os

current = os.getenv(
    "NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
    ).replace("file://", "").replace("%20", " ").strip()

if os.path.isdir(current):
    parent = os.path.abspath(os.path.join(current, os.pardir))
    fldr_path = '"'+current+'"'
    folder = current[current.rfind("/")+1:]
    linkfilename = parent+"/"+folder+"link.desktop"
    linkdata = [
        "[Desktop Entry]",
        "Type=Application",
        "Name=Link to -> "+folder,
        "Icon=folder",
        "Exec=nautilus "+fldr_path,
        ]
    with open(linkfilename, "wt") as wt:
        for l in linkdata:
            wt.write(l+"\n")
    command = "chmod +x "+"'"+linkfilename+"'" 
    subprocess.Popen(["/bin/bash", "-c", command])

How to use

  1. Copy the script into an empty file, save it as make_link (no extension) in ~/.local/share/nautilus/scripts, and make it executable
  2. Use the script by choosing it as a right- click option as in the first image. An executable .desktop file will be created in the same directory, move it elsewhere if you need to; the linked path is absolute.

Give the link an distinguishing icon

You could give the alternative link a distinguishing icon. If you search inside the directory /usr/share/icons for "folder", numerous valid options pop up.

If in the script the line "Icon=folder", is replaced by Icon=stock_folder-copy, (use the icon name without extension), the result on my system is:

enter image description here

Of course you can use your own custom icon as well, but if you use full paths (don't use ~), you should include the icon's extension.

4. Move to the superior directory with a short cut key

Probably the most convenient option; with the nautilus window in front, press a short cut key to move one directory up.

The script

#!/usr/bin/env python3
import subprocess
import time
import os

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8").strip()

def run(cmd):
    subprocess.call(["/bin/bash", "-c", cmd])

# get information on the active window
front = get(["xprop", "-id", get(["xdotool", "getactivewindow"])])
# (only) if it is a "normal" nautilus window, take action
if 'WM_CLASS(STRING) = "nautilus", "Nautilus"' in front:
    # "virtually" press Ctrl + l
    run("xdotool key Control_L+l"); time.sleep(0.05)
    # copy the current path, calculate the "real" parent directory
    real = os.path.realpath(get(["xclip", "-o"]))
    up = os.path.abspath(os.path.join(real, os.pardir))
    time.sleep(0.1)
    # enter the superior directory
    run("printf '"+up+"' | xclip -selection clipboard")
    run("xdotool key Control_L+v")
    # confirm by virtually press Return
    time.sleep(0.1)
    run("xdotool key Return")

To use

  1. For this solution, both xclip and xdotool Need to be on your system.

    sudo apt-get install xdodool xclip
    
  2. Copy the script into an empty file, save it as level_up.py (anywhere).

  3. Add it to a shortcut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/level_up.py
    

NB The shortcut options are a bit limited in this case, since the script itself will simulate Ctrl+L, and Ctrl+Alt+L will make you log out... Ctrl+\ worked fine on my system.

Explanation

This script also simulates Ctrl+L, but instead of using nautilus' "NAUTILUS_SCRIPT_CURRENT_URI", it uses xclip to copy the automatically selected path in the nautilus window. Like option 1, the script then calculates the real path and derives the superior directory.

This option might be useful if you prefer the keyboard to using right- click.

3

Or for Ubuntu 14.04, nautilus 3.10-1, with the xdotool package added, just use the following in your .local/share/nautilus/scripts/updirtree file:

# In nautilus, the pwd is the actual, not the link path
xdotool key ctrl-l
xdotool type "$(dirname $(pwd))" "
"

The final quote should just contain a newline or return (0x0a). The pwd from within nautilus produces a different result than when run from a bash/terminal -- it returns the actual path, not the path using the link.


I agree it makes no sense, it's undocumented, and I can't even figure out what sort of execution environment is running the code (I can't find any shell which produces that result), but it works. It's a hack, which is why I included the version of nautilus. Who knows how long it will work? Might break at the next nautilus upgrade (or the unknown interpreter), but for me, it works on links to mounted locations, links to places in the directory tree, or just plain locations in the directory tree.

  • I use Ubuntu 15.10 ... Can it work, maybe in a similar code? – JohnDoea Jan 18 '16 at 16:25
  • 1
    This makes no sense, $(pwd) is the working directory of the shell, not of nautilus. See help.ubuntu.com/community/NautilusScriptsHowto – Jacob Vlijm Jan 18 '16 at 16:29
  • @benos I confirm, it does work in 15.10 – user.dz Jan 18 '16 at 22:52
  • @JacobVlijm , It works well, it seems to me reasonable, nautilus runs scripts in the current directory. – user.dz Jan 18 '16 at 22:55
  • 1
    Also @Sneetsher to my surprise, it works. You do have a timing issue however when used on very long paths. You'll need to set a break then, depending on the length of the path. Else typed paths appear to be broken. At first, I fixed it with a (arbitrary) break, In the latest version, I made xclip paste the path, which is no subject to the length of the path. – Jacob Vlijm Jan 19 '16 at 7:07
2

A clean fix but needs source rebuild by reverting this commit:

diff --git a/src/nautilus-mime-actions.c b/src/nautilus-mime-actions.c
index ca1f0ac..0b363b4 100644
--- a/src/nautilus-mime-actions.c
+++ b/src/nautilus-mime-actions.c
@@ -2029,21 +2029,13 @@ activate_activation_uris_ready_callback (GList *files_ignore,

    /* Convert the files to the actual activation uri files */
    for (l = parameters->locations; l != NULL; l = l->next) {
-       char *uri = NULL;
-
+       char *uri;
        location = l->data;

        /* We want the file for the activation URI since we care
         * about the attributes for that, not for the original file.
         */
-       if (nautilus_file_is_symbolic_link (location->file)) {
-           uri = nautilus_file_get_symbolic_link_target_uri (location->file);
-       }
-
-       if (uri == NULL) {
-           uri = nautilus_file_get_activation_uri (location->file);
-       }
-
+       uri = nautilus_file_get_activation_uri (location->file);
        if (uri != NULL) {
            launch_location_update_from_uri (location, uri);
        }

Build instructions:

  1. Download source:

    apt-get source nautilus
    
  2. Download build dependencies

    sudo apt-get build-dep nautilus
    
  3. Make the need modification from above patch

    Edit src/nautilus-mime-actions.c

    /* Convert the files to the actual activation uri files */
    for (l = parameters->locations; l != NULL; l = l->next) {
        char *uri = NULL;
        location = l->data;
    
        /* We want the file for the activation URI since we care
         * about the attributes for that, not for the original file.
         */
        if (nautilus_file_is_symbolic_link (location->file)) {
            uri = nautilus_file_get_symbolic_link_target_uri (location->file);
        }
    
        if (uri == NULL) {
            uri = nautilus_file_get_activation_uri (location->file);
        }
    
        if (uri != NULL) {
            launch_location_update_from_uri (location, uri);
        }
    
  4. Build & install it

    autoreconf
    ./configure
    make
    

    To test without installation

    sudo killall -r "[\w]*nautilus"
    ./src/nautilus
    

    To install it

    sudo make install
    

This will make nautilus resolve the links into their target path. BTW, this was reported as bug some time ago. If you think this is a feature then submit another bug report, asking for setting switch or specific shortcut for it.

Reference: How can I stop Nautilus from dereferencing symlinks? [closed]

  • An issue could be that you either need to pin the current version of nautilus, or repeat the procedure after every update. – Jacob Vlijm Jan 16 '16 at 15:41
  • 1
    @JacobVlijm , yep that will be so if i build the deb package. But I didn't, it will be installed /usr/local/bin/, so the system will always run the modified copy, as listed in $PATH – user.dz Jan 16 '16 at 15:44

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.