Command line- rename a series of files

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
PDA123
Posts: 97
Joined: 2021-04-24 01:55
Been thanked: 1 time

Command line- rename a series of files

#1 Post by PDA123 »

Using command line I want to be able to rename a quantity of files to remove
the words and characters;
[Letter]
.0 (dot zero)

Here's the file name-
Robert[Letter]_30.0N_2025.pdf

Here's what I want it to be renamed to;
Robert_30_N_2025.pdf

Any idea how to do it? It must be at command line.

arzgi
Posts: 1614
Joined: 2008-02-21 17:03
Location: Finland
Has thanked: 1 time
Been thanked: 85 times

Re: Command line- rename a series of files

#2 Post by arzgi »

Good to learn to use Debian package utils.

Code: Select all

arto@dell:~$ aptitude search rename
p   arename                      - automatic audio file renaming tool   
p   autorenamer                  - program to rename files to make them 
p   caja-rename                  - Batch renaming extension for Caja    
p   golang-github-google-renamei - provides a way to atomically create o
p   gprename                     - complete batch renamer for Linux     
p   ifrename                     - Rename network interfaces based on va
p   krename                      - powerful batch renamer for KDE       
p   libdevel-caller-ignorenamesp - module for hiding namespaces from cal
p   mp3rename                    - Rename mp3 files based on id3tags    
p   mrename                      - tool for easy and automatic renaming 
p   node-gulp-rename             - gulp pulgin to rename files          
i   rename                       - Perl extension for renaming multiple 
p   rename-flac                  - CLI tool to rename FLAC files        
p   renameutils                  - Programs to make file renaming easier
arto@dell:~$ 

If those can't do, there are sed and awk.

User avatar
Uptorn
Posts: 423
Joined: 2022-01-22 01:07
Has thanked: 331 times
Been thanked: 112 times

Re: Command line- rename a series of files

#3 Post by Uptorn »

For the known characters, you can run rename.

Code: Select all

rename 's/\.0//g'
For pattern matching unknown characters ([Letter]) will require regular expressions which I do not know off the top of my head.

arzgi
Posts: 1614
Joined: 2008-02-21 17:03
Location: Finland
Has thanked: 1 time
Been thanked: 85 times

Re: Command line- rename a series of files

#4 Post by arzgi »

Code: Select all

rename 's/\[Letter\]//' Robert[Letter]_30.0N_2025.pdf
Special characters must be quoted off.

PDA123
Posts: 97
Joined: 2021-04-24 01:55
Been thanked: 1 time

Re: Command line- rename a series of files

#5 Post by PDA123 »

arzgi wrote: 2024-12-11 15:24 Good to learn to use Debian package utils.

Code: Select all

arto@dell:~$ aptitude search rename
p   arename                      - automatic audio file renaming tool   
p   autorenamer                  - program to rename files to make them 
p   caja-rename                  - Batch renaming extension for Caja    
p   golang-github-google-renamei - provides a way to atomically create o
p   gprename                     - complete batch renamer for Linux     
p   ifrename                     - Rename network interfaces based on va
p   krename                      - powerful batch renamer for KDE       
p   libdevel-caller-ignorenamesp - module for hiding namespaces from cal
p   mp3rename                    - Rename mp3 files based on id3tags    
p   mrename                      - tool for easy and automatic renaming 
p   node-gulp-rename             - gulp pulgin to rename files          
i   rename                       - Perl extension for renaming multiple 
p   rename-flac                  - CLI tool to rename FLAC files        
p   renameutils                  - Programs to make file renaming easier
arto@dell:~$ 

If those can't do, there are sed and awk.
Have been using Krename but it's too complicated for a simple user like me.

That's why I need the code for the purpose above.

PDA123
Posts: 97
Joined: 2021-04-24 01:55
Been thanked: 1 time

Re: Command line- rename a series of files

#6 Post by PDA123 »

arzgi wrote: 2024-12-11 16:06

Code: Select all

rename 's/\[Letter\]//' Robert[Letter]_30.0N_2025.pdf
Special characters must be quoted off.
Ok. I used the -n for a test. That's a start. Here's what I tried and the result;

$ rename -n 's/\[Letter\]//' Robert[Letter]_30.0N_2025.pdf

$rename(Robert[Letter]_30.0N_2025.pdf, Robert_30.0N_2025.pdf)

But, it doesn't remove the .0 (dot zero).

Any ideas?

PDA123
Posts: 97
Joined: 2021-04-24 01:55
Been thanked: 1 time

Re: Command line- rename a series of files

#7 Post by PDA123 »

I can do it in two passes (just remove the -n);

rename -n 's/\[Letter\]//' Robert[Letter]_30.0N_2025.pdf

Then...

rename -n 's/\.0//' Robert[Letter]_30.0N_2025.pdf

arzgi
Posts: 1614
Joined: 2008-02-21 17:03
Location: Finland
Has thanked: 1 time
Been thanked: 85 times

Re: Command line- rename a series of files

#8 Post by arzgi »

So, is it a problem? In bash scriprt you can put as many commands as you like. Note still that string is not the same after the first command. Do ls between.

And if it is a requrement, it can be done in one line, but before I would test it does what you are looking for.

PDA123
Posts: 97
Joined: 2021-04-24 01:55
Been thanked: 1 time

Re: Command line- rename a series of files

#9 Post by PDA123 »

arzgi wrote: 2024-12-11 17:35 So, is it a problem? In bash scriprt you can put as many commands as you like. Note still that string is not the same after the first command. Do ls between.

And if it is a requrement, it can be done in one line, but before I would test it does what you are looking for.
The real problem is I have no idea what I'm doing. So, syntax, etc, are meaningless to me.

User avatar
sunrat
Site admin
Site admin
Posts: 7451
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 134 times
Been thanked: 665 times

Re: Command line- rename a series of files

#10 Post by sunrat »

PDA123 wrote: 2024-12-11 16:10 Have been using Krename but it's too complicated for a simple user like me.

That's why I need the code for the purpose above.
I find the Multi-Rename tool in DoubleCommander to be easier to use than KRename. Still won't be something which will work automatically without learning how to use it.
What you are asking is not easily done as a simple click and go function.

So far the offered solutions are good possibilities, sed and awk will indeed do what you want. But also only single commands have been suggested and to do it in bulk will require some simple scripting, maybe easiest with a for loop.
If syntax is meaningless to you, it will be hard to understand and achieve what you want. This is a fairly simple task, do a little research to try to get a basic understanding of the process.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
dilberts_left_nut
Administrator
Administrator
Posts: 5465
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 21 times
Been thanked: 93 times

Re: Command line- rename a series of files

#11 Post by dilberts_left_nut »

'rename' with the right regex and a glob to select files.
Doing it in two passes makes the regex an order of magnitude simpler.
You have excellent examples above, plus search up some regex basics to add understanding.
AdrianTM wrote:There's no hacker in my grandma...

User avatar
kent_dorfman766
Posts: 570
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 69 times
Been thanked: 76 times

Re: Command line- rename a series of files

#12 Post by kent_dorfman766 »

FWIW, the rename command is a PERL script so it uses PERL style REs, which are not 100% POSIX RE interchangeable.

arzgi
Posts: 1614
Joined: 2008-02-21 17:03
Location: Finland
Has thanked: 1 time
Been thanked: 85 times

Re: Command line- rename a series of files

#13 Post by arzgi »

PDA123 wrote: 2024-12-11 23:58 The real problem is I have no idea what I'm doing. So, syntax, etc, are meaningless to me.
rename has been around for guite a long time, easy to find examples from the net.
But hardwire then:

Code: Select all

[arto@dell:~$ mkdir /tmp/ren
arto@dell:~$ cd /tmp/ren
arto@dell:/tmp/ren$ touch Robert[Letter]_30.0N_2025.pdf
arto@dell:/tmp/ren$ ls
'Robert[Letter]_30.0N_2025.pdf'
arto@dell:/tmp/ren$ rename 's/\.0//' Robert[Letter]_30.0N_2025.pdf
arto@dell:/tmp/ren$ ls
'Robert[Letter]_30N_2025.pdf'
arto@dell:/tmp/ren$ rename 's/\[Letter\]//' Robert[Letter]_30N_2025.pdf
arto@dell:/tmp/ren$ ls
Robert_30N_2025.pdf
arto@dell:/tmp/ren$

LouisR4
Posts: 9
Joined: 2021-05-19 17:38

Re: Command line- rename a series of files

#14 Post by LouisR4 »

You can use a for loop with the rename or mv command in the terminal. Here’s a bash example:
bash

Code: Select all

for file in *.pdf; do
  newname=$(echo "$file" | sed 's/\[Letter\]//; s/\.0N/_N/');
  mv "$file" "$newname";
done

Post Reply