Hacker News new | ask | show | jobs
by MrMetric 20 days ago
I've had to batch-rename files many times over the years. That means:

1. I do it manually over however many minutes. Works if there aren't too many (especially if the pattern is too complex to trivially automate).

2. I make a Python script for it. No way I'm renaming a thousand files by hand.

3. I don't do it. Too much work. The problem lingers forever.

Or these days,

4. I make an AI datacenter eat another town's water supply.

I've never used Emacs. I tried vi(m) nonconsensually and had to google how to exit. A while later, I tried it intentionally and hkjl navigation didn't work because I use a custom keyboard layout, so I never touched it again. Sublime Text and its many cursors for the win!

I'd love a way that isn't miserable to do such a common basic task.

2 comments

Try vidir from moreutils (or its improved version, https://github.com/bulletmark/edir ).

It opens the list of filenames in a given directory (or set of files passed on cmdline) in an editor of your choice, and then you use your editor to rename them; the changes get applied when your editor is closed.

> I'd love a way that isn't miserable to do such a common basic task.

The problem is that you actually describe a family of tasks which is not basic. The "iterate over files, select the right ones and apply rules to rename them" part is common; the problem is that the rules vary broadly in kind and complexity, and you haven't figured out how you want to specify them (some ways will be limited in the complexity they can handle.

(Usually the selection of files is trivial; when not, we can fold that complexity into the change rules, and emit null changes in some cases.)

If your selection rule is simple enough for Bash globbing, and your per-file rename rule is simple enough for, say `tr` to handle, then that's trivial to wrap up as a Bash script (or function). In fact, you could write the Python script such that it just accepts a single input filename and outputs the changed version, and handle the rest externally.

> I've never used Emacs. I tried vi(m) nonconsensually and had to google how to exit. A while later, I tried it intentionally and hkjl navigation didn't work because I use a custom keyboard layout, so I never touched it again. Sublime Text and its many cursors for the win!

I'm not really clear on how text editors are supposed to be relevant to batch file renaming. If trying vi(m) the first time wasn't your idea, then there should have been someone else around responsible for guiding you through it. But the variations I've tried had arrow-key navigation configured by default (and `vimtutor` explicitly tells you that it should also work); you really don't have to learn hjkl, which exists largely for a combination of historical reasons with a lot of back-filled justifications. (I would have used ijkl, mimicking an arrow-key layout.) And everything can be remapped in the config files.