tags: #cli #linux #neat-trick
We often rename files to a slight variation (e.g., we may fix a typo, or change a date to a different format, and so on).
Let’s say we wanted to rename
2023/2023-08-28-incremental-differential-full.mkd to
2023/2023-08-28-incremental-differential-or-full.mkd (we’re
adding -or before the -full).
There are several ways to do this in Linux. Using tab completion, you
can type mv 2023/<TAB> (where
<TAB> indicates hitting the TAB key) and find the
right name for the source. Then you can type a space, then again type
2023/<TAB> to get the same name in the target slot of
the mv command, then use your arrow keys and
delete/backspace to fix up the target.
Or you could use a TUI file manager like vifm (which has
enough tricks to power several blog entries!), or use
vidir, etc. More on these in future blogs :)
Meanwhile, here’s a neat trick. First, install the
rlwrap package (should be available on any Linux
distro).
Next, create a file called mvi somewhere in your
$PATH, with the following content:
#!/bin/bash
# usage: $0 some-file
# then edit the "some-file" using readline, hit enter
mv -v "$1" "$(rlwrap -P "$1" head -1)"Now, running mvi some-file-name will show the filename
in a text field editable exactly as you would edit the command line.
Make whatever changes you want, and when you hit enter, the file gets
renamed.
What we’re doing is using rlwrap ability to edit a given
text and using that as the target of the mv command.