Batch renaming with the rename utility
I was in a situation recently where I had a directory full of .whl files I wanted to rename to .zip files. Fortunately, there is the rename utility, which appears to be a thin Perl wrapper.
Given the directory:
In addition, I needed to chop off everything that came after the
first -, e.g.:
asn1crypto.zip cffi.zip cryptography.zip ...
We can use the following rename command:
rename -n 's/([\w]+)-.*/$1.zip/' *.whl
Where:
- The
-n(-nono) flag prevents the utility from actually performing the changes:
-n, -nono
No action: print names of files to be renamed, but
don't rename.
- Parentheses are used to capture the group of characters that match
the enclosed pattern, and
$1is used to refer to the first such captured group *.whldefines the files that will be renamed
Et voilĂ !