pmv - rename files according to perl code
pmv [-vqdmMrh] [-b startcode] [-e endcode]
[--] 'perl code' <files...>
pmv allows you to manipulate filenames based on arbitrary perl code. Think of it
like perl -pe but operating on the filename instead of the file
contents. It is useful for renaming large numbers of files according to
arbitrary criteria.
In the code you write, $_ is set to the
current filename, and you should change $_ to what you
want the new filename to be. This enables you to write quick oneliners
like:
pmv 's/$/.bak/;' *
If you want more control, there are three variables defined:
- $file
- Same as $_, the filename passed in on the command
line.
- $path
- The full pathname, without the filename.
- $name
- The filename, without any path components.
You can alter any of these variables to get the same effect as
altering $_. Which to use is determined as follows:
- 1:
- If $file has been altered, that is used.
- 2:
- If $path or $name has been
altered, they are joined together then used.
- 3:
- Otherwise, $_ is used.
The file pmv-examples.sh contains useful example bourne
shell functions that use pmv. This is available in the source distribution,
and will probably be installed under
/usr/share/doc/mp3-archive-tools/.
This file can be sourced by (using source or .) or
included in one of your startup files (eg /etc/profile or
$HOME/.bash_profile for bash(1) users.
- -v
- Verbose.
- -q
- Quiet (no output). This is the default.
- -d
- Dry run. Shows how it would rename files without actually doing it.
- -b startcode
- Specifies perl code to run once before processing files. Useful for
useing modules and doing one-time initialisation.
- -e endcode
- Specifies perl code to run once after processing files. Useful for
summarising data gathered from filenames.
- -m
- If the pathname has been altered, create any necessary directories.
- -r
- If a file is moved out of a directory, remove any empty directories.
- -M
- Same as -m -r.
- -h
- Show a brief usage summary.
- --
- End of options.
If you have sourced pmv-examples.sh (see above>, the following
functions are available:
- pmv-stdswap
- Swaps round fields in filenames delimited by " - ".
- pmv-space2_
- converts all spaces in filename to underscores.
- pmv-_2space
- Converts all underscores in filename to spaces.
- pmv-fixcaps
- Crude attempt at capitalising filenames.
- pmv-fixcase
- Much better attempt at capitalising filenames. This requires the
Text::Autoformat module from CPAN (www.cpan.org).
- pmv-number
- Numbers filenames sequentially.
- pmv-deepen
- Converts directoriess from "artist - album" to
"artist/album"
- pmv-flatten
- Converts directoriess from "artist/album" to "artist -
album"
- pmv-datestamp
- Insert a datestamp in the form YYYYMMDD at the start of the filename. See
below for more details.
- Convert all whitespace in all mp3 filenames to underscores
-
pmv 's/\s/_/g;' *.mp3
- Convert the spelling of Color to Colour in all filenames
-
pmv 's/Color/Colour/gi;' *
- Swap round fields in mp3 filenames
-
pmv '$name=~s/(.*) - (.*) - (.*) - (.*)(\..*)/$3 - $1 - $2 - $4$5/;' *.mp3
This would convert eg:
Primal Scream - Screamadelica - 04 - Higher Than The Sun.mp3
to:
04 - Primal Scream - Screamadelica - Higher Than The Sun.mp3
- Crudely capitalise every word in all filenames
-
pmv '$name=join(" ",map({ucfirst(lc($_));} split(/\s+/,$name)));' *
See pmv-fixcase in pmv-examples.sh for a better
way to capitalise filenames.
- Add a datestamp to the start of all filenames
-
pmv -b '$d=`date +%Y%d%m`;chomp($d);' '$name = "$d.$name";' *
This adds a datestamp in the form YYYYMMDD (eg
20031214) to the start of a filename. Files with datestamps in this form
will sort in date order, which is useful for (e.g.) logfiles.
The -b code to get the date is run only once, before
the files are processed.
If you wanted to make this into a shell function, callable by
typing "pmv-datestamp files", you
would insert the following into your startup files:
function pmv-datestamp
{
pmv -b '$d=`date +%Y%d%m`;chomp($d);' '$name = "$d.$name";' "$@"
}
Rather similar (I recently discovered) to rename(1), which ships with
perl. However, pmv provides several features that rename does not have, and
the pmv- shell functions in pmv-examples.sh are useful, so I decided to
add it to the mp3-archive-tools(1) package anyway.
perl(1), mp3-archive-tools(1), mp3lint(1)
Ian Beckwith <ianb@nessie.mcc.ac.uk>
pmv is part of the mp3-archive-tools package.
The latest version can be found at:
http://nessie.mcc.ac.uk/~ianb/projects/mp3-archive-tools/
Copyright 2003 Ian Beckwith <ianb@nessie.mcc.ac.uk>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 675 Mass Ave, Cambridge, MA 02139, USA.