Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename multiple files in shell [duplicate]

Tags:

bash

shell

I have multiple files in a directory, example: linux_file1.mp4, linux_file2.mp4 and so on. How do I move these files, using shell, so that the names are file1.mp4, file2.mp4 and so on. I have about 30 files that I want to move to the new name.

like image 540
elzwhere Avatar asked Aug 02 '11 11:08

elzwhere


People also ask

How do I rename multiple files in Linux terminal?

You can also use the find command, along with -exec option or xargs command to rename multiple files at once. This command will append . bak to every file that begins with the pattern “file”. This command uses find and the -exec option to append “_backup” to all files that end in the .

How rename multiple files in Unix?

Rename Multiple Files with the mv Command On its own, the mv command renames a single file. However, combining it with other commands allows you to rename multiple files at the same time. Using this syntax, the find command defines an element of the current file name as the search parameter.


2 Answers

I like mmv for this kind of thing

mmv 'linux_*' '#1' 

But you can also use rename. Be aware that there are commonly two rename commands with very different syntax. One is written in Perl, the other is distributed with util-linux, so I distinguish them as "perl rename" and "util rename" below.

With Perl rename:

rename 's/^linux_//' linux_*.mp4 

As cweiske correctly pointed out.

With util rename:

rename linux_ '' linux_*.mp4 

How can you tell which rename you have? Try running rename -V; if your version is util rename it will print the version number and if it is perl rename it will harmlessly report and unknown option and show usage.

If you don't have either rename or mmv and don't want to or can't install them you can still accomplish this with plain old shell code:

for file in linux_*.mp4 ; do mv "$file" "${file#linux_}" ; done 

This syntax will work with any POSIX sh conforming to XPG4 or later, which is essentially all shells these days.

like image 99
sorpigal Avatar answered Sep 19 '22 03:09

sorpigal


$ rename 's/linux_//' linux_*.mp4 
like image 34
cweiske Avatar answered Sep 19 '22 03:09

cweiske