des*_*ukh 15 filesystem files search tagging
运行 Ubuntu Gnome。
我有很多 PDF 和其他文件,我想标记它们。然后根据这些标签搜索它们。即使我将文件移动到不同的文件夹(因此,标签会粘在文件上)。
我搜索过,但文件和文档不提供此选项。
难道我做错了什么?如何标记文件以便以后可以根据标记搜索它们?
该解决方案由两个脚本组成 - 一个用于标记,一个用于读取特定标签下的文件列表。两者都必须~/.local/share/nautilus/scripts
通过在 Nautilus 文件管理器中右键单击任何文件并导航到脚本子菜单来激活和激活。此处和GitHub 上提供了每个脚本的源代码
这两个脚本都必须保存到~/.local/share/nautilus/scripts
,~
用户的主目录在哪里,并使用chmod +x filename
. 为了便于安装,请使用以下 bash 脚本:
#!/bin/bash
N_SCRIPTS="$HOME/.local/share/nautilus/scripts"
cd /tmp
rm master.zip*
rm -rf nautilus_scripts-master
wget https://github.com/SergKolo/nautilus_scripts/archive/master.zip
unzip master.zip
install nautilus_scripts-master/tag_file.py "$N_SCRIPTS/tag_file.py"
install nautilus_scripts-master/read_tags.py "$N_SCRIPTS/read_tags.py"
Run Code Online (Sandbox Code Playgroud)
标记文件:
在 Nautilus 文件管理器中选择文件,右键单击它们,然后导航到脚本子菜单。选择 tag_file.py
。打Enter
第一次运行这个脚本时,不会有配置文件,所以你会看到:
下次,当您已经标记了一些文件时,您将看到一个弹出窗口,允许您选择一个标签和/或添加新标签(这样您可以在多个标签下记录文件)。点击OK将文件添加到此标签。注意:避免使用“|” 标记名称中的符号。
该脚本将所有内容记录在~/.tagged_files
. 该文件本质上是一个json
字典(这不是普通用户应该关心的东西,但它对程序员来说很方便:))。该文件的格式如下:
{
"Important Screenshots": [
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-10-01 09-15-46.png",
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 18-47-12.png",
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 18-46-46.png",
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 17-35-32.png"
],
"Translation Docs": [
"/home/xieerqi/Downloads/908173 - \u7ffb\u8bd1.doc",
"/home/xieerqi/Downloads/911683\u7ffb\u8bd1.docx",
"/home/xieerqi/Downloads/914549 -\u7ffb\u8bd1.txt"
]
}
Run Code Online (Sandbox Code Playgroud)
如果您想“取消标记”某个文件,只需从该列表中删除一个条目即可。注意格式和逗号。
按标签搜索:
现在您有了一个不错~/.tagged_files
的文件数据库,您可以读取该文件,也可以使用read_tags.py
脚本。
右键单击 Nautilus 中的任何文件(真的无关紧要)read_tags.py
。选择 。打Enter
您将看到一个弹出窗口,询问您要搜索的标签:
选择一项,点击OK。您将看到一个列表对话框,显示您希望文件在您选择的标签中。您可以选择任何单个文件,它将使用分配给该文件类型的默认程序打开。
tag_file.py
:
{
"Important Screenshots": [
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-10-01 09-15-46.png",
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 18-47-12.png",
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 18-46-46.png",
"/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 17-35-32.png"
],
"Translation Docs": [
"/home/xieerqi/Downloads/908173 - \u7ffb\u8bd1.doc",
"/home/xieerqi/Downloads/911683\u7ffb\u8bd1.docx",
"/home/xieerqi/Downloads/914549 -\u7ffb\u8bd1.txt"
]
}
Run Code Online (Sandbox Code Playgroud)
read_tags.py
:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Serg Kolo
# Date: Oct 1st, 2016
# Description: tag_file.py, script for
# recording paths to files under
# specific , user-defined tag
# in ~/.tagged_files
# Written for: http://askubuntu.com/q/827701/295286
# Tested on : Ubuntu ( Unity ) 16.04
from __future__ import print_function
import subprocess
import json
import os
import sys
def show_error(string):
subprocess.call(['zenity','--error',
'--title',__file__,
'--text',string
])
sys.exit(1)
def run_cmd(cmdlist):
""" Reusable function for running external commands """
new_env = dict(os.environ)
new_env['LC_ALL'] = 'C'
try:
stdout = subprocess.check_output(cmdlist, env=new_env)
except subprocess.CalledProcessError:
pass
else:
if stdout:
return stdout
def write_to_file(conf_file,tag,path_list):
# if config file exists , read it
data = {}
if os.path.exists(conf_file):
with open(conf_file) as f:
data = json.load(f)
if tag in data:
for path in path_list:
if path in data[tag]:
continue
data[tag].append(path)
else:
data[tag] = path_list
with open(conf_file,'w') as f:
json.dump(data,f,indent=4,sort_keys=True)
def get_tags(conf_file):
if os.path.exists(conf_file):
with open(conf_file) as f:
data = json.load(f)
return '|'.join(data.keys())
def main():
user_home = os.path.expanduser('~')
config = '.tagged_files'
conf_path = os.path.join(user_home,config)
file_paths = [ os.path.abspath(f) for f in sys.argv[1:] ]
tags = None
try:
tags = get_tags(conf_path)
except Exception as e:
show_error(e)
command = [ 'zenity','--forms','--title',
'Tag the File'
]
if tags:
combo = ['--add-combo','Existing Tags',
'--combo-values',tags
]
command = command + combo
command = command + ['--add-entry','New Tag']
result = run_cmd(command)
if not result: sys.exit(1)
result = result.decode().strip().split('|')
for tag in result:
if tag == '':
continue
write_to_file(conf_path,tag,file_paths)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我找到了一种方法来做到这一点。
打开终端 ( CTRL++ ALT) T,然后运行以下命令:
sudo add-apt-repository ppa:tracker-team/tracker
输入您的密码,出现提示时按 Enter 键,然后运行
sudo apt-get update
然后
sudo apt-get install tracker tracker-gui
如果它说它已经是最新版本,请不要担心。
现在打开 Nautilus/Files 并右键单击要添加标签的文档。选择属性,然后选择标有“标签”的选项卡。在文本框中输入标签并按 Enter 或单击“添加”按钮添加它。您还可以单击已添加的标签,然后选择“删除”按钮来删除标签。请注意,标签区分大小写。您创建的标签将在整个系统中保持不变,因此您可以轻松地在已创建的标签旁边打勾以标记文件,而无需再次手动键入。
标记所需的项目后,您现在可以搜索它们,但不能在文件中搜索。转到活动,然后搜索应用程序Desktop Search
。启动它,然后查看顶部的选项。在窗口的左上角,单击带有工具提示“按列表中的文件显示结果”的文件夹图标。现在您有更多选择。选择搜索框左侧的选项,并显示工具提示“仅在文件标签中查找搜索条件”。现在您可以搜索标签了!
要使用此功能,请输入要搜索的标签,以逗号分隔,然后按 Enter 键。例如:
重要,九月,演示
这只会显示具有所有三个标签的文件:“重要”、“九月”和“演示”。
双击其中一个,它将在默认程序中打开该文件,右键单击并选择“显示父目录”,它将打开它在 Nautilus 中的位置。
在桌面搜索中,您还可以单击窗口顶部右侧的第二个按钮(通常是星形或心形)来编辑应用程序本身的标签!
你有它!希望这可以帮助。如果您还有任何疑问,请告诉我。