drd0sPy
drd0sPy

Reputation: 63

Logrotate and Shell Script

Given logfiles in a directory stamped like this:

log_from_2012_08_14-11:57:21_To_2012_09_14-11:56:12.zip
log_from_2012_10_14-11:57:21_To_2012_11_14-11:56:12.zip

And given that there are about 155450 files like this, how can I do the following in a simple or efficient way:

Use logrotate, or another tool, to make a folder for each month : mkdir $currentMont = october, for example, and put all of the October, 2012 files there, and so on, for each month.

Upvotes: 0

Views: 953

Answers (1)

jmh
jmh

Reputation: 9316

It sounds like a bash script with a couple nested for loops would do the trick.

something like:

for year in $(seq -f "%02g" 0 20); do
    for month in $(seq -f "%02g" 0 12); do
        dest = "20${year}-${month}"
        mkdir "$dest"
        mv "log_from_${dest}*" "${dest}";
    done;
done;

Clearly you would need to tweak this to your needs. If there are too many files in one directory you could run into the line glob limit in which case you will have to use find . | xargs mv to avoid that problem.

Upvotes: 1

Related Questions