Nautilus:通过符号链接访问目录,现在我不能在目标目录层次结构中升级

我通过“make link”选项创建了一个快捷方式。 当我进入这个快捷方式的文件夹时,我看不到它上面的任何文件夹,所以我无法轻松导航到它们。

有没有办法在GUI中运行一个文件夹? 也许是热键? (不能做cd ..这次^ __ ^)。

在此处输入图像描述


例如,在Windows中,我确实能够以我描述的方式导航,这是一个解释它的Win10图像:

在此处输入图像描述

为何这是一个具有挑战性的问题

这个问题有一些挑战:

  • nautilus不直接从命令行进行通信,以获取当前活动的目录,也不能从命令行将当前打开的文件夹(-window)“发送”到另一个目录。
  • 在当前路径中,通过"NAUTILUS_SCRIPT_CURRENT_URI"请求,Nautilus不会返回当前文件夹的实际路径,但“看到”该链接就像它是一个实际文件夹一样。

因此,解决方案就像它得到的一样肮脏; 我们需要找到解决方法。 以下四个选项可以解决问题。

1.右键单击以升级

要获得当前目录的真实路径,我们必须从链接中检索信息。 我们可以通过在链接上使用ls -l来做到这一点,这将输出例如:

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

->之后的部分是符号链接内的真实路径,或者使用python

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

nautilus脚本中使用它,我们可以间接获取当前文件或文件夹的真实路径。

那么如果我们有这条道路,我们如何让鹦鹉螺向上移动一级呢?

同样,我们无法解决这个问题, 保持双手清洁。 要向上移动一级,我们首先编辑找到的路径,从:

 /path/to/a/folder 

 /path/to/a 

然后,使用xdotool模拟Ctrl + L (GUI快捷方式将路径插入nautilus窗口,因为没有cli选项可以使用当前窗口移动到另一个目录),然后使xclip粘贴已编辑的路径+ Enter ,我们有一个有效的解决方案

在实践中

  1. 我们在一个文件夹中,通过桌面上的链接(“链接到电报”)打开。 真实文件夹是我的Downloads文件夹的子文件夹:

    在此处输入图像描述

  2. 然后,如果我们右键单击文件夹中的任何文件来运行脚本:

    在此处输入图像描述

  3. 自动插入上级目录的路径:

    在此处输入图像描述

  4. 并且,还会自动按下Return键,我们将一个目录级别向上移动:

    在此处输入图像描述

剧本

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

如何设置

  1. 该脚本需要xdotoolxclip

     sudo apt-get install xdotool xclip 
  2. 如果目标不存在,则创建目录

     ~/.local/share/nautilus/scripts 
  3. 将上面的脚本复制到一个空文件中,将其保存为~/.local/share/nautilus/scripts level_up (无扩展名),并使其可执行

  4. 您可能必须退出并重新登录。
  5. 现在,您应该能够通过右键单击文件 (任何)> scripts> level_up来运行脚本:

    在此处输入图像描述


[ 编辑 ]我更改了上面的脚本,将路径粘贴nautilus窗口,而不是让xdotool 键入它。 它needs安装xclip ,但它是一项重大改进,特别是在很长的路径上。


2.或者,在上级目录中打开一个新的nautilus窗口

可以通过使脚本在父目录中打开一个新的 nautilus窗口避免使用xdotool 。 然后脚本会更短:

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

在这种情况下,您不需要安装xdotool 。 我们甚至可以通过关闭原始窗口并将新窗口完全放在相同的位置(和大小)来扩展脚本。

缺点是原始窗口的历史以这种方式丢失。

3.另一种解决方案:(自动)创建链接的替代方法

与现有链接无关,但是当从GUI使用时,nautilus脚本在右键单击时自动创建可执行的.desktop文件可能会有所帮助:

右键单击目录以创建快捷方式(表现为链接)

在此处输入图像描述

与符号链接不同,这些链接会将您带到实际的文件夹,而不像文件夹本身那样:

在此处输入图像描述

剧本

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

如何使用

  1. 将脚本复制到一个空文件中,将其保存为~/.local/share/nautilus/scripts make_link (无扩展名),并使其可执行
  2. 使用该脚本,将其选为右键单击选项,如第一张图像中所示。 将在同一目录中创建可执行的.desktop文件,如果需要,将其移动到其他位置; 链接路径是绝对的。

为链接提供一个独特的图标

您可以为备用链接指定一个区别图标。 如果在目录/usr/share/icons搜索“folder”,则会弹出许多有效选项。

如果在脚本中"Icon=folder",行被替换为Icon=stock_folder-copy, (使用不带扩展名的图标名称),我系统上的结果是:

在此处输入图像描述

当然,您也可以使用自己的自定义图标,但如果使用完整路径(不要使用~ ), 则应包含图标的扩展名。

4.使用快捷键移动到上级目录

可能是最方便的选择; 将nautilus窗口放在前面,按一个快捷键将一个目录向上移动。

剧本

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

使用

  1. 对于此解决方案, xclipxdotool需要在您的系统上。

     sudo apt-get install xdodool xclip 
  2. 将脚本复制到空文件中,将其另存为level_up.py (任何位置)。

  3. 将其添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。 单击“+”并添加命令:

     python3 /path/to/level_up.py 

NB在这种情况下,快捷方式选项有点受限,因为脚本本身将模拟Ctrl + L ,而Ctrl + Alt + L将使您注销… Ctrl + \在我的系统上正常工作。

说明

此脚本还模拟Ctrl + L ,但它不使用nautilus“ xclip "NAUTILUS_SCRIPT_CURRENT_URI" ,而是使用xclip复制nautilus窗口中自动选择的路径。 与选项1一样,脚本然后计算实际路径并派生上级目录。

如果您希望键盘使用右键单击,则此选项可能很有用。

或者对于Ubuntu 14.04,nautilus 3.10-1,添加了xdotool包,只需在.local/share/nautilus/scripts/updirtree文件中使用以下内容:

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

最终引用应该只包含换行符或返回值( 0x0a )。 nautilus中的pwd产生的结果与从bash / terminal运行时产生的结果不同 – 它返回实际路径,而不是使用链接的路径。


我同意这没有意义,它没有文档,我甚至无法弄清楚运行代码的执行环境是什么类型的(我找不到任何产生该结果的shell),但它确实有效。 这是一个黑客,这就是为什么我包括了鹦鹉螺的版本。 谁知道它会工作多久? 可能会在下一个nautilus升级(或未知的解释器)中断,但对我来说,它适用于已安装位置的链接,目录树中位置的链接或目录树中的纯位置。

一个干净的修复,但需要通过还原此提交来重建源代码:

 diff --git a/src/nautilus-mime-actions.cb/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); } 

构建说明:

  1. 下载来源:

     apt-get source nautilus 
  2. 下载构建依赖项

     sudo apt-get build-dep nautilus 
  3. 从上面的补丁中修改需要

    编辑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. 构建并安装它

     autoreconf ./configure make 

    无需安装即可测试

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

    安装它

     sudo make install 

这将使nautilus将链接解析为其目标路径。 顺便说一句,这是前段时间报道的bug。 如果您认为这是一项function,请提交另一个错误报告,要求设置切换或特定快捷方式。

参考: 如何阻止Nautilus取消引用符号链接? [关闭]