I received some important files via mail. There are about 150 files like these. The extensions of the files are named as zip1,zip2,zip3, zip_ etc. I just want to change the extension of these files back to zip using a single command to avoid the manual task of renaming each file.
3 Answers
Try:
rename 's/zip[^.]*$/zip/' /path/to/directory/containing/the/files/*
Example:
$ ls
bar.zip2 egg.zip4 foo.zip1 spam.zip3 test.zip_
$ rename 's/zip[^.]*$/zip/' *
$ ls
bar.zip egg.zip foo.zip spam.zip test.zip
This multiple rename task is easy to do using pyRenamer from the default Ubuntu repositories in Ubuntu 16.04 and earlier. The following screenshot explains how to do it:
I know it's not the way to do it in the terminal as you requested, but this is an alternate way of doing it that you may find easier because you can browse to the files to be renamed in the pyRenamer window instead of typing the path.
In Ubuntu 18.04 and later pyRenamer has been replaced by either GPRename or the Smart File Renamer snap package (sudo snap install smart-file-renamer
).
Use mmv
command (Install it by sudo apt-get install mmv
):
mmv -n '*.zip*' '#1.zip'
First * matches everything before .zip and second * matches everything after .zip and they become as group of matches with back-reference #1
and #2
respectively. And we only keep the first one with .zip at the end.
Note: With using -n
option, mmv
command runs as dry, so remove it to perform actual renaming.
Or through combination of find
and mv
commands:
find source/dir -type f -name '*.zip*' -exec sh -c 'echo mv "$1" "${1%.*}.zip"' sh_mv {} \;
- the
source/dir
specifies the path of source directory. -type f
searches files only.-name '*.zip*'
find those type of files that their names are like '*.zip*'{}
represent the current file.- Complete of
"${1%.*}.zip"
, stripes file extension and append.zip
to file name. - So
mv "$1" "${1%.*}.zip"
renames the current file to*.zip
.
Note: Remove echo
command after you ensure about dry run.
-
Why are you using
{}
in that weird way when$0
is anyway being used? O.o– muruMar 26, 2015 at 16:55 -