old-www/LDP/GNU-Linux-Tools-Summary/html/mass-rename.html

443 lines
7.3 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Mass Rename/copy/link Tools</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="GNU/Linux Command-Line Tools Summary"
HREF="index.html"><LINK
REL="UP"
TITLE="Working with the file-system"
HREF="working-with-the-file-system.html"><LINK
REL="PREVIOUS"
TITLE="Working with files and folders"
HREF="working-files-folders.html"><LINK
REL="NEXT"
TITLE="Finding information about the system"
HREF="finding-information.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>GNU/Linux Command-Line Tools Summary</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="working-files-folders.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Working with the file-system</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="finding-information.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="MASS-RENAME"
></A
>7.3. Mass Rename/copy/link Tools</H1
><P
>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 <A
HREF="miscellaneous.html"
>Chapter 19</A
>.</P
><P
>Below are three ways to perform mass renaming of files, using the commands <EM
>mmv</EM
>, <EM
>rename</EM
> (a perl script) or some bash shell scripting.</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
></DT
><DD
><P
> </P
></DD
><DT
>mmv</DT
><DD
><P
><EM
>mmv</EM
> is a mass move/copy/renaming tool that uses standard wildcards to perform its functions.</P
><P
><EM
>mmv's</EM
> manual page is quite difficult to understand, I have only a limited understanding of this tool. However <EM
>mmv</EM
> supports some standard wildcards.</P
><P
>According to the manual the &#8220;;&#8221; wildcard is useful for matching files at any depth in the directory tree (ie it will go below the current directory, recursively).</P
><P
>An example of how to use<EM
> mmv </EM
>is shown below:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>mmv \*.JPG \#1.jpg </PRE
></FONT
></TD
></TR
></TABLE
><P
>The first pattern matches anything with a &#8220;.JPG&#8221; and renames each file (the &#8220;#1&#8221; matches the first wildcard) to &#8220;.jpg&#8221;.</P
><P
>Each time you use a \(wildcard) you can use a #x to get that wildcard. Where x is a positive number starting at 1.</P
><DIV
CLASS="TIP"
><P
></P
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/tip.gif"
HSPACE="5"
ALT="Tip"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>mmv Homepage</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>You can find <EM
>mmv</EM
> on the web <A
HREF="http://linux.maruhn.com/sec/mmv.html"
TARGET="_top"
>here.</A
></P
><P
>Also be aware that certain options used with <EM
>mmv</EM
> are also applicable to other tools in the suite, these include <EM
>mcp</EM
> (mass copy), <EM
>mad</EM
> (mass append contents of source file to target name), <EM
>mln</EM
> (mass link to a source file).</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="TIP"
><P
></P
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/tip.gif"
HSPACE="5"
ALT="Tip"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Tip:</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>A <SPAN
CLASS="APPLICATION"
>Java</SPAN
> alternative to <EM
>mmv</EM
> which runs on both GNU/Linux and <SPAN
CLASS="PRODUCTNAME"
>Windows</SPAN
> is available, <A
HREF="http://www.esomaniac.de/"
TARGET="_top"
>Esomaniac</A
></P
></TD
></TR
></TABLE
></DIV
></DD
><DT
>rename</DT
><DD
><P
><EM
>rename</EM
> is a perl script which can be used to mass rename files according to a regular expression.</P
><P
>An example for renaming all &#8220;.JPG&#8221; files to &#8220;.jpg&#8221; is:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>rename 's/\.JPG$/.jpg/' *.JPG </PRE
></FONT
></TD
></TR
></TABLE
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Finding rename</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>You can get rename from various places. I would recommend trying <A
HREF="http://search.cpan.org"
TARGET="_top"
>CPAN Search Site</A
>, I found the script here <A
HREF="http://search.cpan.org/~pederst/rename-1.4/"
TARGET="_top"
>Rename Script Version 1.4</A
></P
></TD
></TR
></TABLE
></DIV
></DD
><DT
>Bash scripting</DT
><DD
><P
>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 <EM
>mmv</EM
> or <EM
>rename</EM
>...</P
><P
>One way to this is shown below:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>for i in *.JPG;
do mv $i `basename $i JPG`jpg;
done </PRE
></FONT
></TD
></TR
></TABLE
><P
>Note that the above script came from a usenet post. Unfortunately I do not know the author's name.</P
><P
>The first line says find everything with the &#8220;.JPG&#8221; extension (capitals only, because the <SPAN
CLASS="PRODUCTNAME"
>UNIX</SPAN
> system is case sensitive).</P
><P
>The second line uses <EM
>basename</EM
> (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 <EM
>mv</EM
> is run on the output.</P
><P
>An alternative is:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>for i in *.JPG;
do mv $i ${i%%.JPG}.jpg;
done</PRE
></FONT
></TD
></TR
></TABLE
><P
>The above script renames files using a built-in bash function. For more information on bash scripting you may like to see the <A
HREF="http://www.tldp.org/LDP/abs/html/"
TARGET="_top"
>advanced bash scripting guide</A
>, authored by Mendel Cooper.</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="working-files-folders.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="finding-information.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Working with files and folders</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="working-with-the-file-system.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Finding information about the system</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>