[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: changing extensions, a little OT perhaps.



On Fri, 2002-04-26 at 12:32, Bob Torres wrote:
> Hi All,
> 
> I recently transferred some MP3s from my MacOS desktop to my Debianppc
> laptop.  There are no file extensions for these mp3 files, so they're
> kind worthless for xmms in Linux. 
> 
> On the Linux side, how would I go about adding extensions to these files
> without doing it manually?  In other words, is there something I could
> pass at the command line to accomplish this for a whole directory of
> files?  I've been using Linux for three years or so now, and I've always
> wanted to know how to do this...anyone care to educate me?  I'm hoping
> for something a little more substantive than "go learn sed (or whatever
> program)," though learning by example usually works the best for me. ; )


Well sed would be useful to learn, but not as useful as bourne or bash
shell (Bourne is more portable).

If you have all the MP3s in one directory and no other files. Then it is
really easy:

for f in *; do mv "${f}" "${f}.mp3"; done

If you have a directory tree populated with mp3's (And only mp3's) You
can try:

find ~/mp3dir -type f -exec mv '{}' '{}.mp3' \;

Be careful with this one though, as you don't want to accidentally run
it from the wrong directory.

A safer approach might be to make a small shell script:

-- ~/bin/renamemp3
#!/bin/sh

case $# in
1)
	file="$1"
	;;
*)
	echo usage: $0 '<filename>' 1>&2
	exit 1
esac

if ! file "${file}" | grep -q 'MPEG 1.0 layer 3'; then
	echo "${file} is not an mp3 file" 1>&2
	exit 1
fi

case "${file}" in
	*.mp3)
		echo "${file} already has .mp3 suffix" 1>&2
		exit
		;;
	*)
		mv "${file}" "${file}.mp3"
esac
-- ~/bin/renamemp3

Then run:
find ~/mp3dir -type f -exec ~/bin/renamemp3 '{}' \;


I have not tested this script so use with caution..  (IE Read through it
and check that it makes sense, test it on a small dir tree with some
copies of files in it.

--
David Stanaway

Attachment: signature.asc
Description: This is a digitally signed message part


Reply to: