Наши партнеры








Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

Mass Rename/copy/link Tools

There are a few different ways to perform mass renaming of files in GNU/Linux (yes, mass renaming is possible!). There is also a perl script that renames the extentions on files, see Chapter 19.

Below are three ways to perform mass renaming of files, using the commands mmv, rename (a perl script) or some bash shell scripting.

 

mmv

mmv is a mass move/copy/renaming tool that uses standard wildcards to perform its functions.

mmv's manual page is quite difficult to understand, I have only a limited understanding of this tool. However mmv supports some standard wildcards.

According to the manual the “;” wildcard is useful for matching files at any depth in the directory tree (ie it will go below the current directory, recursively).

An example of how to use mmv is shown below:

mmv \*.JPG \#1.jpg 

The first pattern matches anything with a “.JPG” and renames each file (the “#1” matches the first wildcard) to “.jpg”.

Each time you use a \(wildcard) you can use a #x to get that wildcard. Where x is a positive number starting at 1.

mmv Homepage: You can find mmv on the web here.

Also be aware that certain options used with mmv are also applicable to other tools in the suite, these include mcp (mass copy), mad (mass append contents of source file to target name), mln (mass link to a source file).

Tip:: A Java alternative to mmv which runs on both GNU/Linux and Windows is available, Esomaniac

rename

rename is a perl script which can be used to mass rename files according to a regular expression.

An example for renaming all “.JPG” files to “.jpg” is:

rename 's/\.JPG$/.jpg/' *.JPG 

Finding rename: You can get rename from various places. I would recommend trying CPAN Search Site, I found the script here Rename Script Version 1.4

Bash scripting

Bash scripting is one way to rename files. You can develop a set of instructions (a script) to rename files. Scripts are useful if you don't have mmv or rename...

One way to this is shown below:

for i in *.JPG; 
do mv $i `basename $i JPG`jpg; 
done 

Note that the above script came from a usenet post. Unfortunately I do not know the author's name.

The first line says find everything with the “.JPG” extension (capitals only, because the UNIX system is case sensitive).

The second line uses basename (type man basename for more details) with the '$i' argument. The '$i' is a string containing the name of the file that matches. The next portion of the line removes the JPG extension from the end and adds the jpg extention to each file. The command mv is run on the output.

An alternative is:

for i in *.JPG; 
do mv $i ${i%%.JPG}.jpg; 
done

The above script renames files using a built-in bash function. For more information on bash scripting you may like to see the advanced bash scripting guide, authored by Mendel Cooper.