[英]Delete all files except those that contain terms listed in a file
I need to remove all files from a directory that are not partially matched to a list in a text file.我需要从目录中删除与文本文件中的列表不部分匹配的所有文件。
I can't figure out the matching.我无法弄清楚匹配。 The underscore
_
can be used to delimite the match.下划线
_
可用于分隔匹配项。 1
would match with 1_
, but not 11
. 1
将与1_
匹配,但不与11
匹配。
$ cat /files/keep_list.txt
1
22
333
4444
$ ls /files/
1_a.gif # don't delete
1_b.gif # don't delete
1_.jpeg # don't delete
2_c.gif # delete
2_d.gif # delete
2_.jpeg # delete
22_.jpeg # don't delete
23_.jpeg # delete
333_abc.gif # don't delete
4445_123.jpeg # delete
I'd fill GLOBIGNORE
with globs that would match files to be kept and run rm *_*
to get rid of everything else that has an underscore in its name.我会用与要保留的文件匹配的 glob 填充
GLOBIGNORE
并运行rm *_*
以删除名称中包含下划线的所有其他内容。
#!/bin/bash -
mapfile -t parts <keep_list.txt
for part in "${parts[@]}"; do
printf -v GLOBIGNORE '%s:%q_*:*_%q[_.]*' \
"$GLOBIGNORE" "$part"{,}
done
echo rm *_*
Drop echo
if the output looks good.如果 output 看起来不错,则丢弃
echo
。
Not a bash solution, but in python (which should also be available on any linux system):不是 bash 解决方案,而是在 python 中(也应该在任何 linux 系统上可用):
deleteButKeepSome.py: deleteButKeepSome.py:
import os
match = ['1', '22', '333', '4444'] # put the keep_list in here
for root, dirs, files in os.walk('/files'):
for file in files:
if file[:file.find('_')] not in match:
os.remove(f'{root}/{file}')
To run:跑步:
$ python deleteButKeepSome.py
Be careful to execute it in the correct folder!小心在正确的文件夹中执行它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.