The perl
-based rename
utility can use any perl
statements to rename files, not just the s///
search-and-replace operator or the y//
translation operator (just make sure you set $_
to whatever you want the file to be renamed to).
From man rename
:
"rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_
string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.
For example:
rename 'my($b,$e) = m/^(.*)\.([^.]+)$/; $b =~ s/\./_/g; $_="$b.$e"' files
This splits the filenames into base name ($b) and extension ($e). $b then has all periods (.
) replaced with underscores. Finally, $_
is set to "$b.$e"
.
Output (with the -v
verbose option):
$ touch top_axi_256_wrapper_05.02.01.14.00_sample06.tgz \
top_axi_128_wrapper_05.02.01.14.00_sample06.tgz \
a72_a_0.0.0_0.00.04_ac_dc.tgz
$ rename -v 'my($b,$e) = m/^(.*)\.([^.]+)$/; $b =~ s/\./_/g; $_="$b.$e"' *
a72_a_0.0.0_0.00.04_ac_dc.tgz renamed as a72_a_0_0_0_0_00_04_ac_dc.tgz
top_axi_128_wrapper_05.02.01.14.00_sample06.tgz renamed as top_axi_128_wrapper_05_02_01_14_00_sample06.tgz
top_axi_256_wrapper_05.02.01.14.00_sample06.tgz renamed as top_axi_256_wrapper_05_02_01_14_00_sample06.tgz
BTW, use -n
while testing your rename
statements. More from man rename
:
-n, -nono
No action: print names of files to be renamed, but don't rename.
perl rename
is available packaged for most distros (e.g. the rename
package in Debian), or from https://metacpan.org/release/File-Rename