9

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.

  • 1
    Did you forget to quote the wildcard? find /var/log/ -name '*.1' ... – muru Nov 18 '14 at 16:09
  • 1
    I think your should check logrotate for your purpose. – Costas Nov 18 '14 at 16:09
14

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]*'
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]$"
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.