hmmm. I've got a bunch of files, labeled 0013.png through 0171.png, and I need to rename them to 0001.png - 0159.png

this seems like the kind of thing that there'd be an easy oneliner way to do it, but I'm blanking on what it'd be. Any suggestions?

the bad python answer:

import glob,os
[os.rename(orig,f'{1+i:04d}.png') for (i,orig) in enumerate(sorted(glob.glob('*.png')))]

Follow

@foone I think bash globs are sorted, and you can use printf for format strings. So you can do it the same way in bash as you did in python.

· · Web · 0 · 0 · 0
Sign in to participate in the conversation
Awoo Space

Awoo.space is a Mastodon instance where members can rely on a team of moderators to help resolve conflict, and limits federation with other instances using a specific access list to minimize abuse.

While mature content is allowed here, we strongly believe in being able to choose to engage with content on your own terms, so please make sure to put mature and potentially sensitive content behind the CW feature with enough description that people know what it's about.

Before signing up, please read our community guidelines. While it's a very broad swath of topics it covers, please do your best! We believe that as long as you're putting forth genuine effort to limit harm you might cause – even if you haven't read the document – you'll be okay!