I have a list of names in file1 that is a substring of the true filenames of the files I want to delete. I want to delete the files that are partially matched by the names in file1. Any idea how to specify the files to delete?
file1:
file123
file313
file355
True files:
file123.sam.out
file313.sam.out
file355.sam.out
file342455.sam.out
file34455.sam.out
Files to keep:
file342455.sam.out
file34455.sam.out
CodePudding user response:
Assuming you don't have any filenames containing newline literals...
printf '%s\n' * | grep -Fvf file1 | xargs -d $'\n' rm -f --
Let's walk through this piece-by-piece:
printf '%s\n' *
generates a list of files in your current directory.grep -Fvf file1
removes from that list any string that contains as a substring a line fromfile1
xargs -d $'\n' rm -f --
splits its stdin on newlines, and passes anything it's left as an argument torm -f --
If you have GNU tools (and a shell, like bash, with process substitution support), you can use NUL delimiters and thus work with all possible filenames:
printf '%s\0' * |
grep -zFvf <(tr '\n' '\0' <file1) |
xargs -0 rm -f --
printf '%s\0' *
puts a NUL, instead of a newline, after each filename.tr '\n' '\0' <file1
emits the contents of file1 with newlines replaced with NULsgrep -zFvf
reads both its inputs as NUL-delimited, and writes NUL-delimited output, but otherwise behaves as above.xargs -0 rm -f --
reads content, splitting on NULs, and passes input as arguments torm -f --
.
CodePudding user response:
#!/bin/bash
PATTERN_FILE=file1
FILE_TO_REMOVE_FOLDER=files
cat $PATTERN_FILE | while read x
do
if [ "" != "$x" ]
then
echo "rm $FILE_TO_REMOVE_FOLDER/$x*"
rm $FILE_TO_REMOVE_FOLDER/$x*
fi
done