In the zsh
shell, the following would rename the three files shown:
autoload -U zmv
zmv -v -- 'report<1-3>.txt' 'my$f'
The source pattern should be a valid zsh
extended globbing pattern (quoted), and the source pattern used here simply matches the three filenames that you mentioned.
The $f
in the target pattern will be replaced by the full original filename. One could also use $1
, $2
, etc. to refer to any parenthesized capture groups in the source pattern (there are none used here as it's not needed).
From the bash
shell (passing the source and target patterns to the zsh -c
shell as arguments):
zsh -c 'autoload -U zmv; zmv -v -- $1 $2' zsh 'report<1-3>.txt' 'my$f'
This answer ignores the issue of name collisions.
.txt
, or that start withreport
, or just any file that happens to occur in the current directory. If only those three files, thenmv report1.txt myreport1.txt; mv report2.txt myreport2.txt; mv report3.txt myreport3.txt
would do it. How would you want to handle name clashes (one or more of the target names already existing, possibly as a directory)? – Kusalananda Feb 15 '21 at 18:06/
, but may contain newlines, which lines in a text can't contain). – Kusalananda Feb 15 '21 at 18:45rename report myreport report[123].txt
(not tested). This also addresses your aversion of for loops and complexity. – berndbausch Feb 15 '21 at 19:25*
in the most voted on answer (for examplereport*.txt
), then prependmy
in place ofUnix_
. – Kusalananda Feb 15 '21 at 21:37