Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch rename files

Tags:

I want to batch re-name a number of files in a directory so that the preceding number and hypen are stripped from the file name.

Old file name: 2904495-XXX_01_xxxx_20130730235001_00000000.NEW New file name:         XXX_01_xxxx_20130730235001_00000000.NEW 

How can I do this with a linux command?

like image 438
van Avatar asked Sep 11 '13 13:09

van


People also ask

Can you edit multiple file names at once?

Right-click on the first file in the folder, then click “Rename.” 3. Type the new name for the file, then press the Tab key on your keyboard. This will simultaneously save the file's new name, then select the following file so you can instantly start typing a new name for that as well.

How do I bulk rename files in Windows 11?

Select all the files you want to rename. If you don't want to select every file, you can hold the Shift key and individually select the files. Click the Rename button on the toolbar or right-click one of the selected files and then choose the Rename option in the context menu.


1 Answers

This should make it:

rename 's/^[0-9]*-//;' * 

It gets from the beginning the block [0-9] (that is, numbers) many times, then the hyphen - and deletes it from the file name.


If rename is not in your machine, you can use a loop and mv:

mv "$f" "${f#[0-9]*-}" 

Test

$ ls 23-aa  hello aaa23-aa $ rename 's/^[0-9]*-//;' * $ ls aa  hello aaa23-aa 

Or:

$ ls 23-a  aa23-a  hello $ for f in *; > do >   mv "$f" "${f#[0-9]*-}" > done $ ls a  aa23-a  hello 
like image 183
fedorqui 'SO stop harming' Avatar answered Sep 22 '22 04:09

fedorqui 'SO stop harming'