BASH file mass rename with counter
This does what you ask:
n=1; for f in *.txt; do mv "$f" "CO_$((n++))_$f"; done
How it works
n=1
This initializes the variable
n
to 1.for f in *.txt; do
This starts a loop over all files in the current directory whose names end with
.txt
.mv "$f" "CO_$((n++))_$f"
This renames the files to have the
CO_
prefix withn
as the counter. The++
symbol tells bash to increment the variablen
.done
This signals the end of the loop.
Improvement
This version uses printf
which allows greater control over how the number will be formatted:
n=1; for f in *.txt; do mv "$f" "$(printf "CO_%02i_%s" "$n" "$f")"; ((n++)); done
In particular, the %02i
format will put a leading zero before the number when n
is still in single digits.