594 lines
8.4 KiB
HTML
594 lines
8.4 KiB
HTML
<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="book1.htm"><LINK
|
|
REL="UP"
|
|
TITLE="Working with the file-system"
|
|
HREF="c2690.htm"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Working with files and folders"
|
|
HREF="x3289.htm"><LINK
|
|
REL="NEXT"
|
|
TITLE="Finding information about the system"
|
|
HREF="c4268.htm"></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="x3289.htm"
|
|
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="c4268.htm"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="MASS-RENAME"
|
|
></A
|
|
>Mass Rename/copy/link Tools</H1
|
|
><P
|
|
>There are a few different ways to perform mass renaming<A
|
|
NAME="AEN4058"
|
|
></A
|
|
> of files in GNU/Linux (yes, mass renaming is possible!). There is also a perl <A
|
|
NAME="AEN4060"
|
|
></A
|
|
>script that renames <A
|
|
NAME="AEN4062"
|
|
></A
|
|
>the extentions <A
|
|
NAME="AEN4064"
|
|
></A
|
|
>on files, see <A
|
|
HREF="c11270.htm"
|
|
>Chapter 19</A
|
|
>.</P
|
|
><P
|
|
>Below are three ways to perform mass renaming of files, using the commands <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv</I
|
|
></SPAN
|
|
>, <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>rename</I
|
|
></SPAN
|
|
> (a perl <A
|
|
NAME="AEN4070"
|
|
></A
|
|
>script) or some bash <A
|
|
NAME="AEN4072"
|
|
></A
|
|
>shell scripting.<A
|
|
NAME="AEN4074"
|
|
></A
|
|
></P
|
|
><P
|
|
></P
|
|
><DIV
|
|
CLASS="VARIABLELIST"
|
|
><DL
|
|
><DT
|
|
></DT
|
|
><DD
|
|
><P
|
|
> </P
|
|
></DD
|
|
><DT
|
|
>mmv</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN4085"
|
|
></A
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv</I
|
|
></SPAN
|
|
> is a mass <A
|
|
NAME="AEN4088"
|
|
></A
|
|
>move/copy/renaming tool <A
|
|
NAME="AEN4090"
|
|
></A
|
|
><A
|
|
NAME="AEN4092"
|
|
></A
|
|
>that uses standard wildcards <A
|
|
NAME="AEN4094"
|
|
></A
|
|
>to perform its functions.<A
|
|
NAME="AEN4096"
|
|
></A
|
|
></P
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv's</I
|
|
></SPAN
|
|
> manual page is quite difficult to understand, I have only a limited <A
|
|
NAME="AEN4100"
|
|
></A
|
|
>understanding <A
|
|
NAME="AEN4102"
|
|
></A
|
|
>of this tool. However <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv</I
|
|
></SPAN
|
|
> supports <A
|
|
NAME="AEN4105"
|
|
></A
|
|
>some standard <A
|
|
NAME="AEN4107"
|
|
></A
|
|
>wildcards.<A
|
|
NAME="AEN4109"
|
|
></A
|
|
><A
|
|
NAME="AEN4111"
|
|
></A
|
|
></P
|
|
><P
|
|
>According to the manual <A
|
|
NAME="AEN4114"
|
|
></A
|
|
>the “;”<A
|
|
NAME="AEN4116"
|
|
></A
|
|
> wildcard <A
|
|
NAME="AEN4118"
|
|
></A
|
|
>is useful for matching <A
|
|
NAME="AEN4120"
|
|
></A
|
|
>files at any depth <A
|
|
NAME="AEN4122"
|
|
></A
|
|
>in the directory tree (ie it will go below the current directory, recursively).</P
|
|
><P
|
|
>An example of how to use<SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
> mmv <A
|
|
NAME="AEN4126"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
>is shown <A
|
|
NAME="AEN4128"
|
|
></A
|
|
>below:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>mmv \*.JPG \#1.jpg </PRE
|
|
><P
|
|
>The first pattern <A
|
|
NAME="AEN4132"
|
|
></A
|
|
>matches anything with a “.JPG” and renames <A
|
|
NAME="AEN4134"
|
|
></A
|
|
>each file (the “#1” matches the first wildcard) to “.jpg”.</P
|
|
><P
|
|
>Each time you use a \(wildcard) you can use a #x to get that wildcard. Where x is a positive <A
|
|
NAME="AEN4137"
|
|
></A
|
|
>number starting <A
|
|
NAME="AEN4139"
|
|
></A
|
|
>at 1.</P
|
|
><DIV
|
|
CLASS="TIP"
|
|
><BLOCKQUOTE
|
|
CLASS="TIP"
|
|
><P
|
|
><B
|
|
>mmv Homepage: </B
|
|
>You can find <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv</I
|
|
></SPAN
|
|
> 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 <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv</I
|
|
></SPAN
|
|
> are also applicable to other tools in the suite, these include <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mcp</I
|
|
></SPAN
|
|
> (mass copy), <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mad</I
|
|
></SPAN
|
|
> (mass append contents of source file to target name), <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mln</I
|
|
></SPAN
|
|
> (mass link to a source file).</P
|
|
></BLOCKQUOTE
|
|
></DIV
|
|
><DIV
|
|
CLASS="TIP"
|
|
><BLOCKQUOTE
|
|
CLASS="TIP"
|
|
><P
|
|
><B
|
|
>Tip:: </B
|
|
>A <SPAN
|
|
CLASS="APPLICATION"
|
|
>Java</SPAN
|
|
> alternative to <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv</I
|
|
></SPAN
|
|
> 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
|
|
></BLOCKQUOTE
|
|
></DIV
|
|
></DD
|
|
><DT
|
|
>rename</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN4162"
|
|
></A
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>rename</I
|
|
></SPAN
|
|
> is a perl <A
|
|
NAME="AEN4165"
|
|
></A
|
|
>script which <A
|
|
NAME="AEN4167"
|
|
></A
|
|
>can be used to mass rename <A
|
|
NAME="AEN4169"
|
|
></A
|
|
>files according to a regular expression<A
|
|
NAME="AEN4171"
|
|
></A
|
|
>.</P
|
|
><P
|
|
>An example for renaming <A
|
|
NAME="AEN4174"
|
|
></A
|
|
>all “.JPG” files to “.jpg” is:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>rename 's/\.JPG$/.jpg/' *.JPG </PRE
|
|
><DIV
|
|
CLASS="NOTE"
|
|
><BLOCKQUOTE
|
|
CLASS="NOTE"
|
|
><P
|
|
><B
|
|
>Finding rename: </B
|
|
>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
|
|
></BLOCKQUOTE
|
|
></DIV
|
|
></DD
|
|
><DT
|
|
>Bash scripting</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN4186"
|
|
></A
|
|
>Bash <A
|
|
NAME="AEN4188"
|
|
></A
|
|
>scripting is one way to rename <A
|
|
NAME="AEN4190"
|
|
></A
|
|
>files. You can develop <A
|
|
NAME="AEN4192"
|
|
></A
|
|
>a set <A
|
|
NAME="AEN4194"
|
|
></A
|
|
>of instructions <A
|
|
NAME="AEN4196"
|
|
></A
|
|
>(a script) to rename <A
|
|
NAME="AEN4198"
|
|
></A
|
|
>files. Scripts <A
|
|
NAME="AEN4200"
|
|
></A
|
|
>are useful if you don't have <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mmv</I
|
|
></SPAN
|
|
> or <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>rename</I
|
|
></SPAN
|
|
>...</P
|
|
><P
|
|
>One way to this is shown <A
|
|
NAME="AEN4205"
|
|
></A
|
|
>below:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>for i in *.JPG;
|
|
do mv $i `basename $i JPG`jpg;
|
|
done </PRE
|
|
><P
|
|
>Note that the above script <A
|
|
NAME="AEN4209"
|
|
></A
|
|
>came from a usenet <A
|
|
NAME="AEN4211"
|
|
></A
|
|
>post.<A
|
|
NAME="AEN4213"
|
|
></A
|
|
> Unfortunately <A
|
|
NAME="AEN4215"
|
|
></A
|
|
>I do not know the author's name.</P
|
|
><P
|
|
>The first line <A
|
|
NAME="AEN4218"
|
|
></A
|
|
>says find everything <A
|
|
NAME="AEN4220"
|
|
></A
|
|
>with the “.JPG” extension <A
|
|
NAME="AEN4222"
|
|
></A
|
|
>(capitals only, because the <SPAN
|
|
CLASS="PRODUCTNAME"
|
|
>UNIX</SPAN
|
|
> system <A
|
|
NAME="AEN4225"
|
|
></A
|
|
>is case sensitive).</P
|
|
><P
|
|
>The second line uses <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>basename</I
|
|
></SPAN
|
|
> (type man <A
|
|
NAME="AEN4229"
|
|
></A
|
|
>basename for more details) with the '$i' argument.<A
|
|
NAME="AEN4231"
|
|
></A
|
|
> The '$i' is a string <A
|
|
NAME="AEN4233"
|
|
></A
|
|
>containing the name of the file that matches. The next <A
|
|
NAME="AEN4235"
|
|
></A
|
|
>portion of the line removes the JPG <A
|
|
NAME="AEN4237"
|
|
></A
|
|
>extension from the end <A
|
|
NAME="AEN4239"
|
|
></A
|
|
>and adds the jpg <A
|
|
NAME="AEN4241"
|
|
></A
|
|
>extention to each file. The command <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>mv</I
|
|
></SPAN
|
|
> is run <A
|
|
NAME="AEN4244"
|
|
></A
|
|
>on the output.</P
|
|
><P
|
|
>An alternative <A
|
|
NAME="AEN4247"
|
|
></A
|
|
>is:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>for i in *.JPG;
|
|
do mv $i ${i%%.JPG}.jpg;
|
|
done</PRE
|
|
><P
|
|
>The above script renames <A
|
|
NAME="AEN4251"
|
|
></A
|
|
>files using a built-in <A
|
|
NAME="AEN4253"
|
|
></A
|
|
>bash <A
|
|
NAME="AEN4255"
|
|
></A
|
|
>function. For more information <A
|
|
NAME="AEN4257"
|
|
></A
|
|
>on bash <A
|
|
NAME="AEN4259"
|
|
></A
|
|
>scripting <A
|
|
NAME="AEN4261"
|
|
></A
|
|
>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<A
|
|
NAME="AEN4264"
|
|
></A
|
|
><A
|
|
NAME="AEN4266"
|
|
></A
|
|
>.</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="x3289.htm"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="book1.htm"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c4268.htm"
|
|
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="c2690.htm"
|
|
ACCESSKEY="U"
|
|
>Up</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Finding information about the system</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |