11

Im trying to make some backup script as the log files get bigger and bigger. What i have is coping the current file, (for example secure file in /var/log/) and remove the content from that file. But there are some files with the name like: secure.1, secure.2 and all this i like to count them, and if the number is bigger then 2 to archive them all. I can't find the method to find this files or count them. The first think that come up to me was:

find /var/log/ -name *.1 | wc -l

and this will always print 1 as there is one file secure.1. How can i count like in for loop where i can specified a range of numbers like {1..5} or similar. Is there a way to separate this files and make them as one and them backup or delete or what ever ... or first of all how can i find all this numbers that ends up with number.

jimmij
  • 46,064
  • 19
  • 123
  • 136
user3523605
  • 269
  • 2
  • 6
  • 16

3 Answers3

18

With simple -name:

find /var/log -name '*.[2-9]'

or for any digit:

find /var/log -name '*.[[:digit:]]'

or if other chars are possible after digit:

find /var/log -name '*.[2-9]*'
jimmij
  • 46,064
  • 19
  • 123
  • 136
3

To find the filenames which ends with a number ranges from . [1 to 5].

find /var/log/ -type f -regextype sed -regex ".*\.[1-5]$"
Avinash Raj
  • 3,653
  • 4
  • 20
  • 34
1

Why not just...

for log in /var/log/*.[1-5]
do whatever to "$log"
done

You don't need find as far as I can tell - the shell uses the same globs it does in -name. And if all of the files are in a single directory... Of course, if there are subdirectories you're also interested in then find could be beneficial - walking trees in the shell can be a headache.

mikeserv
  • 57,448
  • 9
  • 113
  • 229