这是@blade19899答案的补充。
动机
将文件名放在 .hidden 文件中会隐藏它们,但 nautilus 只检查精确匹配。在某些情况下,我们希望按模式而不是精确名称来隐藏文件--例如,我不喜欢看到 LaTeX 辅助文件。
解决方法
我的解决方法是将 .hidden 分成两个部分,用至少 5 个等号(即 =====)分隔。模式应该放在这个分隔符之前。然后,一个脚本(见下文)会在分隔符之后添加精确的文件名,完成文件。
实际上,您需要手动运行此脚本以更新与新匹配项的文件。也许将此文件添加为 FilemanagerActions 是一个好主意。
脚本
无参数运行:
import glob
from pathspec import PathSpec
def main():
patterns = find_patterns('.hidden')
files = ls()
matches = find_matches(patterns, files)
update_dothidden(patterns, matches)
def find_patterns(filepath):
patterns = []
with open(filepath) as f:
lines = f.readlines()
for line in lines:
if line[0:5] == '=====':
break
patterns.append(line)
return patterns
def ls():
return glob.iglob('**/**', recursive=True)
def find_matches(patterns, files):
path_spec = PathSpec.from_lines('gitwildmatch', patterns)
return filter(path_spec.match_file, files)
def update_dothidden(patterns, matches):
with open('.hidden', 'w') as f:
for pattern in patterns:
f.write(pattern)
f.write('======================\n\n')
for match in matches:
f.write(match + '\n')
f.write("\n# For usage, see https://github.com/m2-farzan/dothidden-patterns")
if __name__ == '__main__':
main()
脚本的最新版本:https://github.com/m2-farzan/dothidden-patterns/blob/main/refresh_dothidden.py
示例
将此内容放入 .hidden
文件中:
*.aux
*.bbl
*.blg
*.log
*.synctex.gz
执行:
~/my_scripts/refresh_dothidden.py
更新的.hidden文件:
*.aux
*.bbl
*.blg
*.log
*.synctex.gz
======================
dstar_main.aux
main.aux
main.bbl
main.blg
main.log
main.synctex.gz
mrpp_central.aux
tikzhead.aux