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
.
$ 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.
#!/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.
Not a bash solution, but in python (which should also be available on any linux system):
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.