-1

I have many file with name chr1_gene_*.raw. I would like to keep some of them. So I use following command.

find . -maxdepth 1 -type f -name "*.raw" -not -name "chr1_gene_448.raw" -not -name "chr1_gene_1914.raw" -not -name "chr1_gene_2456.raw" -not -name "chr1_gene_1554.raw" -not -name "chr1_gene_2024.raw" -not -name "chr1_gene_35.raw" -not -name "chr1_gene_509.raw" -not -name "chr1_gene_1952.raw" -not -name "chr1_gene_575.raw" -not -name "chr1_gene_2249.raw" -not -name "chr1_gene_272.raw" -not -name "chr1_gene_2158.raw" -exec rm -rf {} \;

Sometimes there are too many files I want to keep. I do not want to type "-not -name " too many times. Is there a way to put a list in "-not -name"?

Mike Brown
  • 331
  • 2
  • 12
  • You could put the filenames into a list, then do a `find *.raw | grep -v -f list | xargs rm -rf`. That is filter name out with `grep -v` from the list and forward the filtered filelist to xargs for the deletion. – Lars Fischer Apr 23 '16 at 11:40

1 Answers1

0

You may achieve this using a script say notnamescript.sh :

#!/bin/bash
while read line
do
echo "-not -name " $line
done<notnamelist

Put all the -not -name names in a file called notnamelist. Remember there should be no trailing empty lines.

 find . -maxdepth 1 -type f -name "*.name" $( ./notnamescript.sh ) -exec rm -rf {} \;
sjsam
  • 21,411
  • 5
  • 55
  • 102