[ale] quick renaming hiccup help

Michael B. Trausch mike at trausch.us
Wed Nov 30 10:55:44 EST 2011


On Thu, Nov 24, 2011 at 12:37:47PM -0500, mute wonder wrote:
> I tried to remove part of a common pattern from a group of folders
> with a quick 'rename' command.  It didn't work.
>
> Within the same folder, several subfolders have names containing "
> -- Jamendo - MP3 VBR", and I wanted to remove everything in the
> names that began with that string.  So, I tried the rename command:
>
>  rename \ --\ Jamendo* _folder *Jamendo\ -\ MP3*
>
> And this tacked the intended replacement text, "_folder" onto the
> end of the intended removal text, " -- Jamendo*", but it managed to
> match all the intended folders.  So escaping the spaces seemed to
> work only part of the time.  Why?

I am guessing that you meant "mv" here...

The mv command (which is used to actually move files as well as rename
them) cannot perform actions on multiple files unless the very last
argument is a directory.

In order to perform a rename operation on multiple files, you're going
to want to leverage the power of your shell and basic utilities.  For
example, to convert all "--" to "-" in your files in the current
directory:

 for old_file in *; do \
    NEW_FILE="$(echo $old_file|sed 's/--/-/g;')"; \
    mv "$old_file" "$NEW_FILE"; done

Now, there is a potential issue here; if you have any files that start
with one or more - characters, and because the shell expands arguments
for the programs on a UNIX system (e.g., the shell has the
responsibility for parsing the command line and doing substitutions
and globbing, applications do not) then you need to guard against
that.  Why?  Well, imagine that you are running the "rm" program and
you have a file in the current directory named "-Rf"...

So, to do that (this works on GNU, not sure about other systems):

 for old_file in *; do \
    NEW_FILE="$(echo $old_file|sed 's/--/-/g;')"; \
    mv -- "$old_file" "$NEW_FILE"; done

The "--" says "end of options" and is honored by all GNU software that
I am aware of, and most other programs as well that use the GNU option
parser library.  It means that no matter what the name of a file is,
it won't be interpreted as an option, ever.  This is important for the
same reason that it is important that we place the variables
representing the file names inside of the double-quotes to guard
against breaking names at whitespace characters (not just spaces!).

	--- Mike


More information about the Ale mailing list