How do I move numbers in filenames, in a batch renaming operation?
I have been trying to figure out how to rename files for the past few hours.
I have 2000 files that are like this:
file.1.pdb
file.2.pdb
file.3.pdb
I would like to rename these files to something like:
file.pdb.1
file.pdb.2
file.pdb.3
If you have rename
installed, you can use
rename -n 's/(\.\d+)\.pdb$/.pdb$1/' *.pdb # just watch what WOULD happen
rename 's/(\.\d+)\.pdb$/.pdb$1/' *.pdb # actually rename the files
The command rename
can be installed via
sudo apt install rename
Through mmv
(rename multiple files by wildcard patterns) it's mush easy:
mmv '*.*.*' '#1.#3.#2' *.pdb
or zmv
of zsh
shell; it's a module that allows to do rename; see ZMV-Examples:
zmv -w '*.*.*' '$1.$3.$2' *.pdb
Using Perl rename
:
rename -n 's/(\.\d+)(\.pdb)/$2$1/' *.pdb
Quick explanation:
-
*.pdb
Match all files that end with.pdb
. (Done by the shell) -
(\.\d+)
Match a literal dot, then one or more decimal digits. The parens create a match group. -
$2$1
Reverse the first and second match groups. -
-n
No action (simulate). If the output looks good, run the command again without this flag.