[CLUE-Tech] Args gripe (was mpg123 - recursively play directories?)

Jed S. Baer thag at frii.com
Tue Aug 27 10:23:33 MDT 2002


On 27 Aug 2002 08:58:34 -0600
Dale Hawkins <dhawkins at cdrgts.com> wrote:

> "Jed S. Baer" <thag at frii.com> writes:
> 
> 
> > find /path/to/mp3-root | xargs ...
> 
> My biggest complaint about xargs, is if there are any special
> characters in the input.  For example, many of my mp3 files have
> apostrophes and xargs gets confused.  The only method I know to deal
> with the problem is to filter all the file names through a nasty sed
> expression to escape all special characters.
> 
> Any better or more general solution to this problem?

Yeah, ban Windoze. ;-)

Truthfully, it's more of a general GUI issue, I suppose. Folks who use
GUIs tend to worry less about shell special characters. The filenames I
really have to chuckle at are those resulting from serving up filenames
containing spaces via a webserver, resulting in
the%20name%20of%20the%20file.mp3, for example. The urlencoded '#' shows up
often too.

There might be a bash (or choose your shell) environment variable to
disable special character processing. Off the top of my head, I can't
think of it. I think there's also an option when invoking the shell to
deal with this -- at least partially, but I don't think you'd want to do
that with an interactive shell.

I ran across this problem in spades when I migrated a large pile of stuff
from my Win95 box to Linux. I wound up writing an awk script to just
rename everything, translating various special characters to underscores,
and just ignoring others, like the tilde. Eventually, I found it so
useful, that I embodied it as a Perl function (here listed as part of a
script).

Invoke the following Perl script by cd'ing to the directory containing the
files, and issuing:

  ls --color=no | fixdosnames

< fixdosnames >
#!/usr/bin/perl -w
#
# Author: Jed S. Baer
#
# License: Perl Artistic License
#
sub fixnames {

  shift;

# get rid of URL-encoded characters

  s/\%[0-9]{2}/_/g;

# convert to lower, and get rid of punctuation marks

  tr/A-Z/a-z/;
  tr' -~!@#$%^&*(){}|\":;?<>,[]+='_'s;
  tr/'/_/s;

# collapse multiple underscores, remove leading underscores

  s/_+/_/g;
  s/^_+//g;

  return $_
}

while (<>) {

  chomp;
  $oldname = $_;
  $newname = fixnames($oldname);

  if ($oldname ne $newname) {
    # tell the user what is happening
    print "mv -- $oldname $newname\n";
    @sargs = ("mv", "--", "$oldname", "$newname");
    system(@sargs) == 0 or print
      "unable to move file $oldname: $?\n";
  }

}

Note that using the system call avoids running the command via the shell,
so special characters aren't an issue. But, using "--" as the first
argument negates having mv take the first argument as an option, just in
case the filename starts with a hyphen (yes, I've had those).

Yeah, I know, it really doesn't "fix" the problem, unless you just want to
be permanently rid of the special characters in filenames, for all shell
processing of those files.

jed
-- 
We're frogs who are getting boiled in a pot full of single-character
morphemes, and we don't notice. - Larry Wall; Perl6, Apocalypse 5



More information about the clue-tech mailing list