If we can assume that all your files are in the same directory, that none of the files can have the same name (e.g. you don't have plt500
and already have plt000500
), all names consist of nothing but plt
and then the number, and all files starting with plt
need to be renamed, then you can do this:
for f in plt*; do
numNoZero=$(sed 's/^0*//' <<<"${f/plt/}")
echo mv -- "$f" "$(printf 'plt%06d' "$numNoZero")";
done
First, we iterate over all files and directories (letus know if you need to skip directories) whose name starts with plt
, saving each as $f
. Next, ${f/plt/}
is the file name with the plt
removed, so it should be just the number. We feed this through a sed
command that removes any leading 0s, in case you have any, and save the result (the number without leading 0s) as $numNoZero
. Next, we use printf
, telling it t print plt
and then the number, padded to 6 digits (%06d
) and use this as the name we want to mv
to.
If this looks right, remove the echo
and re-run to actually rename the files.
Alternatively, if you have Perl rename
(known as rename
on Ubuntu and other Debian systems, perl-rename
or prename
in others), you can just do:
rename -n 's/plt0*(\d+?)$/sprintf("plt%06d", "$1")/e' plt*