This should work on my CentOS 6.6 but somehow the file name is not changed. What am I missing here?
rename -f 's/silly//' sillytest.zi
This should rename sillytest.zi
to test.zi
but the name is not changed. Of course I can use mv
command but I want to apply to many files and patterns.
There are two different rename
utilities commonly used on GNU/Linux systems.
util-linux version
On Red Hat-based systems (such as CentOS), rename
is a compiled executable provided by the util-linux
package. It’s a simple program with very simple usage (from the relevant man page):
rename from to file...
rename
will rename the specified files by replacing the first occurrence of from
in their name by to
.
Newer versions also support a useful -v, --verbose
option.
NB: If a file already exists whose name coincides with the new name of the file being renamed, then this rename
command will silently (without warning) over-write the pre-existing file.
Example
Fix the extension of HTML files so that all .htm
files have a four-letter .html
suffix:
rename .htm .html *.htm
Example from question
To rename sillytest.zi
to test.zi
, replace silly
with an empty string:
rename silly '' sillytest.zi
Perl version
On Debian-based systems ,rename
is a Perl script which is much more capable
as you get the benefit of Perl’s rich set of regular expressions.
Its usage is (from its man page):
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
rename
renames the filenames supplied according to the rule specified as the first argument.
This rename
command also includes a -v, --verbose
option. Equally useful is its -n, --no-act
which can be used as a dry-run to see which files would be renamed. Also, it won’t over-write pre-existing files unless the -f, --force
option is used.
Example
Fix the extension of HTML files:
rename s/\.htm$/.html/ *.htm