使用Nautilus将文件与包含文本的剪贴板进行比较
该答案主要用于将文件与剪贴板中从Internet复制的文本进行比较。剪贴板中的文本可能已经从系统上的另一个文件中复制了,这使它成为合格的答案。
使用bash的本机diff
命令突出显示文件差异,然后使用显示差异gedit
。但是,可以将其修改为meld
或任何其他第三方程序包。
选择文件后,此答案使用Nautilus的内置函数来运行自定义脚本:
#!/bin/bash
# NAME: clipboard-diff
# PATH: $HOME/.local/share/nautilus/scripts
# DESC: Find differences bewteen selected file on disk and clipboard.
# CALL: Called from Nautilus file manager.
# DATE: March 18, 2017. Modified: March 31, 2017.
# NOTE: The clipboard would contain text highlighted on website and copied
# with <ctrl>+<C>. Requires command `xclip` to be installed.
# Must have the xclip package. On Ubuntu 16.04, not installed by default
command -v xclip >/dev/null 2>&1 || { zenity --error --text "Install xclip using: 'sudo apt install xclip' to use this script. Aborting."; exit 99; }
# strip new line char passed by Nautilus
FILENAME=$(echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | sed -e 's/\r//g')
# Multiple files can't be selected.
LINE_COUNT=$(wc -l <<< "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS")
LINE_COUNT=$((LINE_COUNT-1))
if [[ $LINE_COUNT > 1 ]] ; then
zenity --error --text "Ony one file can be selected at a time! "
exit 1
fi
# Object type must be "file..." (ie no directories, etc.)
if [ -d "${FILENAME}" ] ; then
zenity --error --text "$FILENAME is a directory!";
exit 1
else
if [ -f "${FILENAME}" ]; then
: # Bash noop
else
zenity --error --text "${FILENAME} is not a file!";
exit 2
fi
fi
# Get clipboard contents into working file
workfile="/tmp/clipboard-work-"$(date +%s)
xclip -o > $workfile
# Create temporary file name so two or more open instances won't clash
differences="/tmp/clipboard-diff-"$(date +%s)
# Compare file differences
# -q brief -B ignore blank lines, -u only differences
diff --unified=2 -w -b -B -I --suppress-blank-empty \
--suppress-common-lines --ignore-all-space \
${FILENAME} $workfile > $differences
# If file doesn't exist, errors in diff parameters
# If file size =0 there were no differences
if [[ -f $differences ]] ; then
if [[ -s $differences ]] ; then
# File not empty.
gedit $differences
else
zenity --info --text "$workfile matches $differences"
fi
else
zenity --error --text "cliboard-diff - error in diff parameters."
fi
# clean up /tmp directory
rm $workfile
rm $differences
exit 0
注意:我几周前开发了此Nautilus脚本,一直想将其发布为新的问答集,但时间紧迫,不确定是否有人真的会对它感兴趣。
样品输出
在此示例中,我们将2017年3月31日之前在澳大利亚发布的实际脚本与2017年3月31日修订的版本进行比较。请注意如何设置新信息和错误消息。
该diff
命令非常强大,因此具有无数的控制参数。键入man diff
在所述终端针对所述手册的页面或info diff
更多更命令的用法的细节。