'delete files with partial match to another list of names
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
Solution 1:[1]
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 file1removes from that list any string that contains as a substring a line fromfile1xargs -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' <file1emits the contents of file1 with newlines replaced with NULsgrep -zFvfreads 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 --.
Solution 2:[2]
#!/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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
