Deleting files in Linux with a loop using `rm`

Posted on

Problem :

I have files like file.log.1.gz, file.log.2.gz, …, file.log.100.gz. I want to have a loop from 1 to 100 to delete these files. How can this be done?

I tried this command in Linux:

for i in $(seq 1 100); do rm -rf file.log.$i.gz

Solution :

Your done is missing.

for i in $(seq 1 100); do rm -f file.log.$i.gz; done

I removed -r because recursion is useless when the argument is a file.

This loop-less syntax should also work in bash. It feeds all the names to a single rm:

rm file.log.{1..100}.gz

Leave a Reply

Your email address will not be published. Required fields are marked *