Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename multiple files from one extension to another in Linux / Unix?

I have a number of files that end in a '.1', for example:

example.file.ex1.1
example.file.ex2.1
example.file.ex3.1

Is there a way that I can quickly rename them all without the '.1' at the end (e.g. example.file.ex1, example.file.ex2, etc.)?

Thanks!

like image 389
user2716340 Avatar asked Aug 25 '13 22:08

user2716340


People also ask

How do you change the extension of all files in a folder in Linux?

Change File Extensions From the Terminal And if you want to change the extension (or the name), you'd use the mv command. mv stands for "move" and is the standard command on Linux for moving and renaming files.


2 Answers

Yes, try this with rename :

rename -n 's/\.1$//' *

remove the -n (dry-run mode switch) if your tests are valid.

warning There are other tools with the same name which may or may not be able to do this, so be careful.


If you run the following command (linux)

$ file $(readlink -f $(type -p rename))

and you have a result like

.../rename: Perl script, ASCII text executable

then this seems to be the right tool =)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo update-alternatives --set rename /path/to/rename

Last but not least, this tool was originally written by Larry Wall, the Perl's dad.

like image 88
Gilles Quenot Avatar answered Nov 15 '22 21:11

Gilles Quenot


Pure bash solution:

for curFile in example.file.*.1; do
    mv -- "$curFile" "${curFile:0:-2}"
done
like image 22
Aleks-Daniel Jakimenko-A. Avatar answered Nov 15 '22 20:11

Aleks-Daniel Jakimenko-A.