Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove substring from filename

Tags:

shell

ubuntu

I have files with name of the form "NAME-xxxxxx.tedx" and I want to remove the "-xxxxxx" part. The x are all digits. The regex "\-[0-9]{1,6}" matches the substring, but I have no idea how to remove it from the filename.

Any idea how I can do that in the shell?

like image 912
wiseveri Avatar asked Apr 01 '26 14:04

wiseveri


1 Answers

If you have the perl version of the rename command installed, you could try:

rename 's/-[0-9]+//' *.tedx

Demo:

[me@home]$ ls
hello-123.tedx  world-23456.tedx
[me@home]$ rename 's/-[0-9]+//' *.tedx
[me@home]$ ls
hello.tedx  world.tedx

This command is smart enough to not rename files if it means overwriting an existing file:

[me@home]$ ls
hello-123.tedx  world-123.tedx  world-23456.tedx
[me@home]$ rename 's/-[0-9]+//' *.tedx
world-23456.tedx not renamed: world.tedx already exists
[me@home]$ ls
hello.tedx  world-23456.tedx  world.tedx
like image 107
Shawn Chin Avatar answered Apr 03 '26 16:04

Shawn Chin