13

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
Eliah Kagan
  • 117,780

6 Answers6

16

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
PerlDuck
  • 13,335
11

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.
wjandrea
  • 14,236
  • 4
  • 48
  • 98
11

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
αғsнιη
  • 35,660
8

You can use this script:

for i in `seq 1 2000`; do
mv file.$i.pdb file.pdb.$i
done

Or this copy-paste friendly command:

for i in `seq 1 2000`; do mv file.$i.pdb file.pdb.$i; done

For use above commands, put all 2000 files in one folder and then open terminal in that directory, then run above command in it.

5

Recently nautilus, the default file manager, received a batch rename dialog. It is not yet powerful enough to do what you want. But luckily there is thunar, an alternative file manager that can be installed. With thunars rename dialog you can do what you want using the GUI.

First install thunar:

sudo apt install thunar

Start thunar, navigate to the directory that has your files. Then select all of them. Press F2.

In the dialog that opens, again, select all files. Change mode to "Search & Replace" and to "Name & Suffix". Check "Regular Expression". Now use the following as search and replace patterns:

file\.(.+)\.pdb

and

file.pdb.$1

Finally click the rename button.

The advantage of this way is that you get a visual preview of what will happen before you actually do the renaming.

thunar rename dialog

Sebastian Stark
  • 6,122
  • 19
  • 48
  • 1
    You can make most programs print English messages by prefixing the command with LC_MESSAGES=POSIX, e. g. LC_MESSAGES=POSIX free. Works the same for graphical applications although in the case of persistent applications like Nautilus in its default configuration you need to terminate the previous instance with nautilus -q first. – David Foerster Mar 30 '18 at 16:00
  • @DavidFoerster thanks for mentioning, image replaced. – Sebastian Stark Mar 30 '18 at 22:50
3

You can use rename from util-linux for this (the command is called rename.ul in ubuntu):

rename.ul .pdb '' *
rename.ul "file." "file.pdb." *

This first removes the .pdb extension from the end and then re-inserts it into the middle.

  • 1
    Pretty cool. I wasn't aware of rename.ul. The syntax is a bit, um, unexpected, but it actually works the way you advertise. Plus: it also has a -n (don't touch, just tell) switch. – PerlDuck Mar 29 '18 at 17:40