8 logrotate-counters - Increment numbers in file names
19 .
/usr
/lib
/tool
/bash-utils
23 warnx
"This command is to increment counters in file names."
24 warnx
"Usage: $0 [--dry-run] [basename-1 [basename-2 [...]]]"
25 warnx
"Where basename is the file name (eg. \"access.log\") on which numbers in all \"access.log.NUMBER.*\" files are gonna be incremented."
31 if [ "$1" = --dry-run ]
45 basename_length
=${#basename}
46 for filename
in "$basename".
+([0-9]).
*
48 if [ -n "${filename//[!$TAB]/}" -o -n "${filename//[!$LF]/}" ]
50 warnx
"Tab or linefeed char in filename - skipping it: $filename"
54 ending
=${filename:$basename_length}
55 if [[ $ending =~ ^\.
([0-9]+)\.
(.
+)$
]]
57 counter
=${BASH_REMATCH[1]}
58 remainder
=${BASH_REMATCH[2]}
59 newname
=$basename.$
[counter
+1].
$remainder
60 echo "$counter $filename $newname"
64 while read -r counter filename newname
68 echo "$filename" '->' "$newname"
70 mv -n -v "$filename" "$newname"