Listing: albumize
#!/bin/sh
if [ -z "$ALBUM" ]; then
echo 'You must set the ALBUM name first (eg. export ALBUM="Greatest Hits")'
exit 1
fi
for x in *; do
echo -n $x; echo -ne '\000'
echo -n `echo $x|cut -f 1 -d '-'`
echo -n " - $ALBUM - "
echo -n `echo $x|cut -f 2- -d '-'`; echo -ne '\000'
done | xargs -0 -n2 mv
We're actually doing two tricky things here. First,
we're building a list consisting of the original
filename followed by the name to which we'd like to
mv it, separated by NULL characters, for all
files in the current directory. We then feed that entire list to an
xargs with two switches: -0
tells it to break on NULLs (instead of newlines or whitespace), and
-n2 tells it to take two arguments at a time on
each pass, and feed them to our command (mv).
Save the script as ~/bin/albumize. Before you
run it, set the $ALBUM environment variable to the name that
you'd like injected into the filename just after the
first -. Here's a trial run:
rob@catlin:~/Music/Hallucinogen - The Lone Deranger$ export ALBUM="The Lone Deranger"
rob@catlin:~/Music/Hallucinogen - The Lone Deranger$ albumize
rob@catlin:~/Music/Hallucinogen - The Lone Deranger$ ls
Hallucinogen - The Lone Deranger - 01 - Demention.mp3
Hallucinogen - The Lone Deranger - 02 - Snakey Shaker.mp3
Hallucinogen - The Lone Deranger - 03 - Trancespotter.mp3
Hallucinogen - The Lone Deranger - 04 - Horrorgram.mp3
Hallucinogen - The Lone Deranger - 05 - Snarling (Remix).mp3
Hallucinogen - The Lone Deranger - 06 - Gamma Goblins Pt. 2.mp3
Hallucinogen - The Lone Deranger - 07 - Deranger.mp3
Hallucinogen - The Lone Deranger - 08 - Jiggle of the Sphinx.mp3
rob@catlin:~/Music/Hallucinogen - The Lone Deranger$
What if you would like to remove the album name again? Try this one,
and call it ~/bin/dealbumize:
#!/bin/sh
for x in *; do
echo -n $x; echo -ne '\000'
echo -n `echo $x|cut -f 1 -d '-'`; echo -n ' - '
echo -n `echo $x|cut -f 3- -d '-'`; echo -ne '\000'
done | xargs -0 -n2 mv
and simply run it (no $ALBUM required):
rob@catlin:~/Music/Hallucinogen - The Lone Deranger$ dealbumize
rob@catlin:~/Music/Hallucinogen - The Lone Deranger$ ls
Hallucinogen - 01 - Demention.mp3
Hallucinogen - 02 - Snakey Shaker.mp3
Hallucinogen - 03 - Trancespotter.mp3
Hallucinogen - 04 - Horrorgram.mp3
Hallucinogen - 05 - Snarling (Remix).mp3
Hallucinogen - 06 - Gamma Goblins Pt. 2.mp3
Hallucinogen - 07 - Deranger.mp3
Hallucinogen - 08 - Jiggle of the Sphinx.mp3
rob@catlin:~/Music/Hallucinogen - The Lone Deranger$
The -0 switch
is also popular to team up with the -print0
option of find (which, naturally, prints matching filenames separated
by NULLs instead of newlines). With find and
xargs on a pipeline, you can do anything you
like to any number of files, without ever running into the dreaded
Argument list too long error:
rob@catlin:~/Pit of too many files$ ls
bash: /bin/ls: Argument list too long
A find/xargs combo makes
quick work of these files, no matter what they're
called:
rob@catlin:/Pit of too many files$ find -type f -print0 | xargs -0 ls
To delete them, just replace that trailing ls
with an rm, and away you go.
i tried to remove them, however i ran into the ARG_MAX limit - neither ls or rm would tolerate so many files. interestingly enough, the solution suggested in this article did not work either, refusing to work for the same reason. i ended up doing the following:
shell#find ./ -name "*8*log -print0| xargs -0 rm
this reduced the number of files to less than 130 thousand. only then was i able to use the solution suggested in this article.
either way, thanks and props to the author for a great piece!
kindest regards,
ggolin