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?
The rm command is used to delete files.
The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command.
The two options are equivalent, by default, rm does not remove directories. And by using the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.
All non-hidden files and directories in your home directory will be deleted. Any contents of any userfs -mounted partitions (networked or otherwise) will be deleted.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With