Reputation: 2385
I want to remove all files that do not match R1.fastq.gz
in my list of files.
How do I use rm
with inverse match?
Upvotes: 7
Views: 5857
Reputation: 18
ls | grep -v "file_with_string_you_dont_want_to_delete" | xargs rm -f
Upvotes: 0
Reputation: 530823
Use the extended pattern syntax available in bash
:
shopt -s extglob
printf '%s\n' !(R1.fastq.gz) # To verify the list of files the pattern matches
rm !(R1.fastq.gz) # To actually remove them.
Or, use find
:
find . ! -name R1.fastq.gz -print # Verify
find . ! -name R1.fastq.gz -exec rm {} + # Delete
If your version of find
supports it, you can use -delete
instead of -exec rm {} +
:
find . ! -name R1.fastq.gz -delete
Upvotes: 15
Reputation: 1025
You may want the "invert match" option, grep –v
, which means "select lines which do not match the regex." and then remove them.
Something like rm $(ls | grep -v -e "R1.fastq.gz")
should do it.
Please, note that this will erase all files in the folder you are on, except R1.fastq.gz
Upvotes: -1