Take this senario. You have been asked to create a support file, but due to the number of rotated logs, the support file is very large. In the source directory, there are many files with the name messages.nn. Some of these files go back days, months or even years. You want to remove these files in one go:
To delete all files with the name “messages.nn”, older than 5 days::
# cd /var/log
# find messages.* -type f -mtime +5 | xargs rm
To check what files will be deleted, exclude “| xargs rm” from the above command.
Another way (courtesy of the Mr. L. Gurru):
This way actually defines the numbers on the end of the file.
Firstly, to test that it returns the correct files:
# ls -al messages.[1-2][0-9]
Back to pre-school maths, [1–2] defines the “tens“, whereas [0–9] defines the “units“. They are read right to left. This will list the files messages.10 to messages.29, assuming files messages.10 to messages.29 exist. If you wanted to include messages.30, the “tens” would be [1-3].
To list messages.0 to messages.9, exclude the “tens”: messages.[0-9]
To list messages.123, add the the “hundreds” unit: messages.[1-2][0-9]
And, to delete:
# rm messages.[1-2][0-9]