I'd use tools designed for batch renaming as they're good at making sanity checks before doing anything or have dry-run modes that show you what they would do instead of doing them.
There are several of them, the most popular ones being perl's rename
, a very short example script that used to be shipped with perl
since the 80s but has still evolved into something more advanced (and several variants thereof). That one can use all the power of perl
's string processing capabilities, so the possibilities are endless.
There's also an unrelated but very limited command called rename
in the util-linux
package.
mmv
is a dedicated batch renaming tool, also from the 80s. It used to be quite popular but seems to have fallen out of fashion lately.
zsh
comes with a zmv
auto-loadable function that can use the full power of zsh filename generation and expansion operators.
Here, your task is relatively trivial as it's just a matter of removing the first 4 characters of files that start with MD18
, so:
rename 's/^MD18//' MD18*.log
mmv 'MD18*.log' '#1.log'
autoload zmv
zmv 'MD18(*.log)' '$1'
You can also always use a loop in simpler shells like bash
:
for file in MD18*.log; do
mv -i "$file" "${file#MD18}"
done
But it won't have any of the safeguards of the specialised tools. Here we're still using -i
though to give a chance to the user to abort before a file is lost/overridden in the process. A dry run can also be performed by prepending the command with echo
.
rename -n 's/^....//' *.log
and if it looks OK, remove the option-n
and rename the files. Myrename
is linked to Larry Wall's perl scriptfile-rename
. But ... beware of potential collisions while renaming! – sudodus Aug 23 '21 at 15:34