![在 nautilus 中显示符号链接目标](https://linux22.com/image/1147968/%E5%9C%A8%20nautilus%20%E4%B8%AD%E6%98%BE%E7%A4%BA%E7%AC%A6%E5%8F%B7%E9%93%BE%E6%8E%A5%E7%9B%AE%E6%A0%87.png)
是否可以将 Nautilus 中的符号链接目标显示为文件夹列表中的一列?请不要指望我ls -l
,我确实知道并使用它,但如果 UI 用户有一种 UI 方式来查看列表中的链接目标,那就太好了。
答案1
此目标可以通过 Nautilus Script 实现。
该答案的来源是问题的答案部分:如果我创建了一个文件夹的链接,那么如何在 Nautilus 中从该链接文件夹转到“真实”文件夹?
该脚本的来源如下:在 Nautilus 中打开链接目标。列出的依赖项是perl
和zenity
,但它们已默认安装。
脚本内容没有区别,但是在现在的 Ubuntu 版本中,Nautilus 脚本的位置路径更改为:~/.local/share/nautilus/scripts
1.创建脚本文件并使其可执行:
touch ~/.local/share/nautilus/scripts/"Open link target"
chmod +x ~/.local/share/nautilus/scripts/"Open link target"
2.编辑脚本文件:
gedit ~/.local/share/nautilus/scripts/"Open link target" &
3.粘贴下几行作为脚本的内容:
#!/bin/bash
#Title=Open-the-link-target-in-nautilus
#Title[fr]=ouvrir-le-repertoire-cible-dans-nautilus
#==============================================================================
# open-the-link-target-in-nautilus
#
# author : SLK
# version : v2011051501
# license : Distributed under the terms of GNU GPL version 2 or later
#
#==============================================================================
#
# description :
# nautilus-script :
# opens the target of a symbolic link of the selected object; if
# the target of the symbolic link is a file, opens the parent folder
#
# informations :
# - a script for use (only) with Nautilus.
# - to use, copy to your ${HOME}/.gnome2/nautilus-scripts/ directory.
#
# WARNINGS :
# - this script must be executable.
# - package "zenity" must be installed
#
#==============================================================================
#==============================================================================
# CONSTANTS
# 0 or 1 - 1: doesn't open but displays a message
DRY_RUN=0
#------> some labels used for zenity [en]
z_title='Open the link target in nautilus'
z_err_bin_not_found='not found\nEXIT'
z_no_object='no object selected\nEXIT'
z_info_target='path of the target'
z_choice_open_nautilus='open target in nautilus'
z_choice_open_file='open file with default application'
z_choice_display_filepath='open a messagebox to copy filepath'
#------> some labels used for zenity [fr]
#z_title='ouvrir le repertoire cible dans nautilus'
#z_err_bin_not_found='introuvable\nEXIT'
#z_no_object='aucun objet selectionne\nEXIT'
#z_info_target='chemin de la cible'
#z_choice_open_nautilus='ouvrir la cible dans nautilus'
#z_choice_open_file='ouvrir le fichier avec le programme par defaut'
#z_choice_display_filepath='ouvrir une boite de dialogue affichant le chemin du fichier'
#==============================================================================
# INIT VARIABLES
# may depends of your system
DIRNAME='/usr/bin/dirname'
GREP='/bin/grep'
NAUTILUS='/usr/bin/nautilus'
PERL='/usr/bin/perl'
READLINK='/bin/readlink'
XDG_OPEN='/usr/bin/xdg-open'
ZENITY='/usr/bin/zenity'
#==============================================================================
# FUNCTIONS
function check_bin
{
err=0
for bin in $* ; do
if [ ! -x "$bin" ] ; then
$ZENITY --error --title "$z_title" \
--text="$bin $z_err_bin_not_found"
err=1
fi
done
[ $err -eq 1 ] && exit 1
}
#==============================================================================
# MAIN
# lets check for required binaries :
[ -x "$ZENITY" ] || {
echo "[ERROR] $ZENITY not found : EXIT"
exit 1
}
check_bin "$DIRNAME" "$GREP" "$NAUTILUS" "$PERL" "$READLINK"
# lets check if object is selected :
[ "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" == "" ] && {
$ZENITY --error --title "$z_title" \
--text="$z_no_object"
exit 1
}
# retrieve the first object selected :
first_object=`echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" \
| $PERL -ne 'print;exit'`
# lets check if local path :
[ `echo "$first_object" | $GREP -c "^/"` -eq 0 ] && {
$ZENITY --error --title "$z_title" \
--text="[ERROR] $first_object has not a valid path\nEXIT"
exit 1
}
# retrieve the target path :
if [ -L "$first_object" ] ; then
# symbolic link
target=`$READLINK -f "$first_object"`
else
# not a symbolic link :
target="$first_object"
fi
if [ -d "$target" ] ; then
# target is a directory
target_to_open_in_nautilus="$target"
else
# target is a file, let's take the parent directory
target_to_open_in_nautilus=`$DIRNAME "$target"`
fi
### DRY RUN : noop
[ $DRY_RUN -eq 1 ] && {
$ZENITY --info --title "$z_title" \
--text="<b>DRY RUN</b>
first_object: $first_object
target: $target
target_to_open_in_nautilus: $target_to_open_in_nautilus"
exit 0
}
### GO : let's open
choice=`$ZENITY --list --title="$z_title" --width="500" --height="200" \
--text="<b>$z_info_target</b>\n$target" \
--radiolist --column "" --column "action" \
TRUE "$z_choice_open_nautilus" \
FALSE "$z_choice_open_file" \
FALSE "$z_choice_display_filepath"`
case $choice in
"$z_choice_open_nautilus")
$NAUTILUS --no-desktop "$target_to_open_in_nautilus"
;;
"$z_choice_open_file")
$XDG_OPEN "$target"
;;
"$z_choice_display_filepath")
$ZENITY --entry --title="$z_title" --width="500" \
--text="$z_info_target" \
--entry-text="$target" &
;;
*)
exit 1
;;
esac
exit 0
### EOF
4.保存文件,打开 Nautilus,右键单击某些符号链接,转到“脚本部分”,然后单击“打开链接目标”:
5.然后就会出现这个对话框:
恐怕如果您想在列表视图中添加新列,就必须更改并重新编译 Nautilus 的源代码。
答案2
pa4080 提供的答案在 Ubuntu 16.04 上对我来说效果很好。但是,对于文件链接,我发现如果新的 Nautilus 窗口打开时选择了目标文件,则会更有帮助。这类似于在 Nautilus 中执行查找时,如果右键单击其中一个搜索结果并选择“打开项目位置”。
为了实现这一点,我替换了以下部分
if [ -d "$target" ] ; then
# target is a directory
target_to_open_in_nautilus="$target"
else
# target is a file, let's take the parent directory
target_to_open_in_nautilus=`$DIRNAME "$target"`
fi
和
target_to_open_in_nautilus="$target"
现在,当链接是一个文件时,新的 Nautilus 窗口将打开并且链接的目标被选中。