Renaming files in lexicographical order with a numerical pattern that increases sequentially with fixed padding

I have files such as

a
bb
ccc
abc
emrls

I would like to rename them so that, after sorting them (for example, lexicographically), each file gets the following name:

00001
00002
00003
etc

where the # of digits for the 0 padding is specified a priori, e.g. 6 in the case above (assuming that we know how many digits are needed).

Since the shell I am most comfortable with is zsh, I’m interested in solutions that benefit from features on zsh (maybe using zsh‘s zmv?). I would also be interested in solutions that are compatible with Bash.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use the l parameter expansion flag to pad a number on the left.

i=0; for x in *; do ((++i)); mv -- $x new/${(l:6::0:)i}; done

There is a relatively simple way to do this with only POSIX features: start numbering at 1000001 (for 6 digits) instead of 1, and strip off the leading 1. It is less straightforward but a few characters shorter.

i=1000000; for x in *; do i=$((i+1)); mv -- "$x" new/${i#1}; done

If you want to take advantage of zmv, you can use an arithmetic expression that increments i inside the replacement text.

i=0; zmv '*' '${(l:6::0:)$((++i))}'
i=1000000; zmv '*' '${$((++i))#1}'

Add the o glob qualifier if you need to sort the files in a different order. With zmv, you need to pass the -Q flag when the pattern contains glob qualifiers.

Method 2

Use this bash snippet.

[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc9f999288938fbc9f999288938f">[email protected]</a> new]$ touch a bb ccc dddd eee f gh i
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7f797268736f5c7f797268736f">[email protected]</a> new]$ touch abc emrls cdg sf
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0665636872697546656368726975">[email protected]</a> new]$ touch ABC A BB CCC DD GI KLM kmna kabc mas nas san fin zoo
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89b9d968c978bb89b9d968c978b">[email protected]</a> new]$ ls -1
a
A
abc
ABC
bb
BB
ccc
CCC
cdg
DD
dddd
eee
emrls
f
fin
gh
GI
i
kabc
KLM
kmna
mas
nas
san
sf
zoo
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0162646f756e724162646f756e72">[email protected]</a> new]$ a=0; for i in *; do a=$(($a+1));  b=`printf "%06d" $a`; mv -v ${i} ${b};  done
`a' -> `000001'
`A' -> `000002'
`abc' -> `000003'
`ABC' -> `000004'
`bb' -> `000005'
`BB' -> `000006'
`ccc' -> `000007'
`CCC' -> `000008'
`cdg' -> `000009'
`DD' -> `000010'
`dddd' -> `000011'
`eee' -> `000012'
`emrls' -> `000013'
`f' -> `000014'
`fin' -> `000015'
`gh' -> `000016'
`GI' -> `000017'
`i' -> `000018'
`kabc' -> `000019'
`KLM' -> `000020'
`kmna' -> `000021'
`mas' -> `000022'
`nas' -> `000023'
`san' -> `000024'
`sf' -> `000025'
`zoo' -> `000026'
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f093959e849f83b093959e849f83">[email protected]</a> new]$


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x