Batch Rename Files in Linux Easily(批量重命名linux)

It is always something of a juggling act to keep up our file system tidy. We need to name files in a meaningful way, ensuring that all the files are organized in such a way that they are easily retrievable. One such annoying task is to rename multiple files in a directory in Linux. Doing this manually could be tedious and time-consuming, however, the inbuilt command line tool ‘rename’ can come to your rescue here!

The ‘rename’ tool uses Perl expressions to rename multiple files at once in a given directory. It searches a directory containing files with a pattern and renames them as per another given replacement pattern.

Let us take a look at a sample example. Suppose you have a bunch of files in the current working directory like following:

Apples2021Mar.pdf

Orange2021Mar.pdf

Pear2021Mar.pdf

Now, you might want to rename all the above files to ”Fruit2021Mar.pdf”. To achieve this renaming, all you need to do is execute the below command in Terminal:

`rename ‘s/[A-Za-z]+/Fruit/’ *.pdf`

What this command does is that it searches for all the .pdf files in the current directory and replaces the pattern [A-Za-z]+ for the pattern “Fruit” which is the desired word. Hence, all the files will be renamed to ”Fruit2021Mar.pdf”.

Having said about renaming files, you might also be interested in renaming the file extensions in a directory. Suppose you have some .jpg files in a directory and you might want to rename all of them to .JPG files or want to rename .mp3 files to .MP3. This can also be achieved using the ‘rename’ command in Linux.

Let us consider the following example. Suppose you have alist of files with their names as following:

Song1.mp3

Song2.mp3

Song3.mp3

Now, you can batch rename all these files to uppercase extensions using the following command:

`rename ‘s/\.mp3/.MP3/’ *.mp3`

This command searches for all the mp3 files in the current directory and renames them as MP3 making their extensions uppercase.

In fact, the ‘rename’ command line tool can be used for all kinds of renaming operations, from adding suffixes to a group of files to changing the names of files. Hopefully, the example above has given you a glimpse of how helpful this command line tool can be to efficiently and quickly rename multiple files at once.


数据运维技术 » Batch Rename Files in Linux Easily(批量重命名linux)