mmv — move multiple files by wildcard patterns 

Introduction

If you ever have to rename or move multiple files under Linux, then the mmv©2007 dsplabs.com.au shell command will make your life easy. The mmv utility will also come in handy for copying multiple files, appending content of one file to other files, as well as for creation of links.

Installation

While mmv©2007 dsplabs.com.au is typically not included as part of base build for Linux distros, it should be included on the installation media. If not, then you will have to use your favourite package manager (yum, yast, apt, smart, etc.) to install it from the web.

Synopsis

A brief synopsis of the mmv©2007 dsplabs.com.au command, including the most commonly used switches and parameters, is given below:

mmv [-m|r|c|o|a|l|s] [-h] [-v|n] [from to]

       -m : move source file to target name
       -r : rename source file or directory to target name
       -c : copy source file to target name
       -o : overwrite target name with source file
       -a : append contents of source file to target name
       -l : link  target name to source file
       -s : same as -l, but use symbolic links instead of hard links

       -v : verbose mode
       -n : no-execute mode

       -h : help information

*see the man pages for full description of available switches

Example usage

You can run the following examples from any Linux shell, I will use bash. Lets start with some simple examples. To change extensions of all files in the current directory from .jpeg  to .jpg, use:

mmv "*.jpeg" "#1.jpg" # ©2007 dsplabs.com.au"

Notice, how in the above example, the match for a * wildcard in the first argument (source/from pattern) gets used in the second argument (destination/to pattern) via #1.

Similarly, if we have more than one wildcard, the match for the second wildcard can be used in the destination pattern with #2, and so on. For example, the following command will insert an underscore after the first occurrence of the word page in filenames with extensions starting with htm:

mmv "*page*.htm*" "#1page_#2.htm#3" # ©2007 dsplabs.com.au"

It is also possible to include entire path-names with from/to wildcard patterns:

mmv "./foo_*/bar/*.txt" "./foo_#1/#2.txt" # ©2007 dsplabs.com.au"

In the above example, all .txt files contained in bar sub-directory of any foo_* parent directory, are moved one level up, i.e. into their corresponding foo_* directories.

You can run recursion on files in sub-directories, like so:

mmv -r "foo*/*.txt" "~#2.txt" # ©2007 dsplabs.com.au"

mmv also allows for case conversion. For example, to lowercase all of the files in the current directory, use:

mmv "*" "#l1" # ©2007 dsplabs.com.au"

To upercase all the files and directories in the current directory, include the -r switch:

mmv -r "*" "#u1" # ©2007 dsplabs.com.au"

To append contents of all .txt files in the present directory to an output all.txt file, use:

mmv -a "*.txt" "all.txt" # ©2007 dsplabs.com.au"

With verbose mode enabled (-v switch), the above command displays the following information:

mmv -v -a "file?.txt" "all.txt" # ©2007 dsplabs.com.au"
    file1.txt -> all.txt : done
    file2.txt -> all.txt : done
    file3.txt -> all.txt : done

For further details consult the man pages:

man mmv # ©2007 dsplabs.com.au

Other tools

There are also other ways of performing multiple file operations. These might be useful if, for whatever reason, mmv©2007 dsplabs.com.au is unavailable.

To change extensions from .abc to .123, use one of the following:

for i in *.abc; do mv "$i" "${i/\.abc/.123}"; done
for i in *.abc; do mv $i `echo $i|sed 's/\.abc$/.123$/'`; done # ©2007 dsplabs.com.au
for i in *; do mv $i `basename $i .abc`.123; done # ©2007 dsplabs.com.au

The following bash one-liners convert filenames to lowercase:

for f in `find *`; do mv "$f" "`echo "$f" | tr A-Z a-z`"; done # ©2007 dsplabs.com.au
for f in `find . -type f -name "*[A-Z]*"`; do mv "$f" "`echo "$f" | tr A-Z a-z`"; done # ©2007 dsplabs.com.au

Did you find the above information useful and interesting? If so, please support this site by using the blog directory links at the bottom of this page. Thanks for your support!

If you have any Linux related problems or questions then please feel free to post them on our Linux Forums: http://linux.dsplabs.com.au/forums.




VPS Hosting Referral Code DZZCC3

Add me to Technorati Favorites Vote for me on Blog Catalog

7 Responses to “mmv — move multiple files by wildcard patterns”

  1. dfi Says:

    for i in *.abc; do mv $i `echo $i|sed 's/.abc$/.123$/'`; done
    can be done in bash with:
    for i in *.abc; do mv "$i" "${i/.abc/.123}"; done

    If you omit the " (as you do in your expression) you will not treat filenames with spaces correctly, furthermore the regex is incorrect . is a wildcard in your expression - it should be escaped vie \.

    Cheers,
    Konrad

  2. Kamil Says:

    Hi Konrad,

    Thank you for your comment with useful information. I have made the suggested changes.

    Cheers,
    Kamil

  3. Working script, Would like someone to look at for me - The UNIX and Linux Forums Says:

    […] can get it from any other repo I have had a hard time finding just a source package for it though. Linux Blog mmv — move multiple files by wildcard patterns […]

  4. goodsoul Says:

    try also:
    rename 's/.abc$/.123$/' *.abc

  5. Bill Says:

    Thank you! Your bash 1 liner does the trick as I dont have mmv in cygwin by default. Woot!

  6. ge spacemaker microwave Says:

    ge spacemaker microwave…

    […]Linux Blog » mmv — move multiple files by wildcard patterns[…]…

  7. Crawl Space Guru Says:

    Crawl Space Guru…

    […]Linux Blog » mmv — move multiple files by wildcard patterns[…]…

Leave a Reply