简体   繁体   English

删除所有文件,但包含文件中列出的术语的文件除外

[英]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.

相关问题 删除除模式列表文件之外的所有文件 - delete all files except a pattern list file 如何删除目录中除一个文件夹和一个文件以外的所有文件? - how to delete all files in directory except one folder and one file? Unix:如何删除文件中列出的文件 - Unix: How to delete files listed in a file 如何查找除文件所有者名称中具有特定子字符串的文件之外的所有文件 - How to find all files except those with particular substring in owner name of file 删除 bash 脚本中除最新的 3 个文件之外的所有文件 - Delete all files except the newest 3 in bash script 删除所有文件和目录-除非指定 - Delete all files and directories - except specified 删除除子目录中最新的10个文件外的所有文件 - Delete all files except the latest 10 in subdirectories shell脚本删除除不同文件夹中的最后更新文件以外的所有文件 - shell script to delete all files except the last updated file in different folders 删除除每个前缀的最近n个文件以外的所有文件(基于文件日期)? - Delete all except the most recent n files of each prefix (based on file date)? 如何删除除最后 2 个目录之外的所有目录以及指向这些目录的符号链接 - How to delete all directories except last 2 directories and the symlink pointed to those directories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM