This is an extension to @blade19899's answer
Motivation
Putting file names in .hidden file will hide them but nautilus only checks for exact matches. In some scenarios we want to hide files by pattern, not by exact names—for example I don't like to see latex aux files.
Workaround
My workaround is to split the .hidden in two sections
separated by a line of at least 5 equal signs (i.e. =====). The patterns
should be placed before this separator. Then, an script (see below) completes
the file by adding exact filenames after the separator.
In practice, you need to run this script manually to update the file
with new matches. Maybe adding this file as a FilemanagerActions would
be a good idea.
Script
Run with no arguments:
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()
Latest version of the script: https://github.com/m2-farzan/dothidden-patterns/blob/main/refresh_dothidden.py
Example
Put this into .hidden file:
*.aux
*.bbl
*.blg
*.log
*.synctex.gz
Execute:
~/my_scripts/refresh_dothidden.py
Updated .hidden file:
*.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