Question

how can i do this in a loop?

I need to put little script in cron-tab which will take actual logs and put them in log-1 file. Rename log-1 file to log-2 file ... Bassicaly i need to have week old log files, sorted in log-1 to log-7 logs, where log-7 is day log 7 days ago and updated daily.

Bassicaly i need this in a little loop and have a problem with syntax:

mv log-6 log-7
mv log-5 log-6
mv log-4 log-5
mv log-3 log-4
mv log-2 log-3
mv log-1 log-2
mv logactual log-1
Was it helpful?

Solution

You can do:

for ((i=7; i>=2; i--)); do mv "log-$((i-1))" "log-$i"; done; mv logactual log-1
mv log-6 log-7
mv log-5 log-6
mv log-4 log-5
mv log-3 log-4
mv log-2 log-3
mv log-1 log-2
mv logactual log-1

OTHER TIPS

While I agree your question shows some lack of effort, I could not resist the challenge. ;)

for file in log-*
do
    lognr=$(echo $file | sed "s/log-\(\d*\)/\1/")
    mv "$file" "log-$(expr $lognr + 1)"
done
mv logactual log-1

Thank you for answers, for lack of effort case, i had:

COUNTER=7
until [  $COUNTER -gt 0 ]; do
mv log-($COUNTER -1) log-$COUNTER
let COUNTER-=1
done

didn't know how to do that log-($COUNTER -1) and had trouble googling it out

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top