rename files

rename files

am 08.09.2004 15:57:47 von d0x

can any one help me out with this. I know somebody must have done it
already.

i have a directory with 901 files in it.
i need a script the renames the files. basically i need it to take
anything that is not a number,letter, or "." and change it to an "_".

ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"


perl/sh/bash, whatever, dosn't matter. :)

Thanks

Re: rename files

am 08.09.2004 16:10:09 von Stephane CHAZELAS

2004-09-08, 09:57(-04), d0x:
> can any one help me out with this. I know somebody must have done it
> already.
>
> i have a directory with 901 files in it.
> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
[...]

Use zsh4.

autoload -U zmv # if not already in ~/.zshrc
cd /directory
zmv -n '(**/)(*)' '$1${2//[^.[:alnum:]]/_}'

Remove "-n" if you're happy with what zmv is going to do.

--
Stephane

Re: rename files

am 08.09.2004 16:10:09 von Stephane CHAZELAS

2004-09-08, 09:57(-04), d0x:
> can any one help me out with this. I know somebody must have done it
> already.
>
> i have a directory with 901 files in it.
> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
[...]

Use zsh4.

autoload -U zmv # if not already in ~/.zshrc
cd /directory
zmv -n '(**/)(*)' '$1${2//[^.[:alnum:]]/_}'

Remove "-n" if you're happy with what zmv is going to do.

--
Stephane

Re: rename files

am 08.09.2004 16:56:34 von cfajohnson

On 2004-09-08, d0x wrote:
> can any one help me out with this. I know somebody must have done it
> already.
>
> i have a directory with 901 files in it.
> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
>
> perl/sh/bash, whatever, dosn't matter. :)

In bash2, ksh93:

for file in *
do
case $file in
*[!a-zA-Z0-9_.]*)
newfile=${file//[!a-zA-Z0-9_.]/_}
mv "$file" "$newfile"
;;
esac
done

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
============================================================ =======
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Re: rename files

am 08.09.2004 16:56:34 von cfajohnson

On 2004-09-08, d0x wrote:
> can any one help me out with this. I know somebody must have done it
> already.
>
> i have a directory with 901 files in it.
> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
>
> perl/sh/bash, whatever, dosn't matter. :)

In bash2, ksh93:

for file in *
do
case $file in
*[!a-zA-Z0-9_.]*)
newfile=${file//[!a-zA-Z0-9_.]/_}
mv "$file" "$newfile"
;;
esac
done

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
============================================================ =======
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Re: rename files

am 08.09.2004 18:53:32 von Joe Smith

d0x wrote:

> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
>
> perl/sh/bash, whatever, dosn't matter. :)

That's a one-liner in perl.

unix% perl -e 'for(@ARGV){$o=$_;tr/a-zA-Z0-9./_/c;rename $o,$_}' *

-Joe

Re: rename files

am 08.09.2004 18:53:32 von Joe Smith

d0x wrote:

> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
>
> perl/sh/bash, whatever, dosn't matter. :)

That's a one-liner in perl.

unix% perl -e 'for(@ARGV){$o=$_;tr/a-zA-Z0-9./_/c;rename $o,$_}' *

-Joe

Re: rename files

am 08.09.2004 21:02:49 von William Park

In d0x wrote:
> can any one help me out with this. I know somebody must have done it
> already.
>
> i have a directory with 901 files in it.
> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
>
>
> perl/sh/bash, whatever, dosn't matter. :)

Assuming there is no leading/trailing whitespaces or newlines,
/bin/ls -d *[^a-zA-Z0-9_]* | while read i;
j=${i//[^a-zA-Z0-9_]/_}
mv "$i" "$j"
done

--
William Park
Open Geometry Consulting, Toronto, Canada

Re: rename files

am 08.09.2004 21:02:49 von William Park

In d0x wrote:
> can any one help me out with this. I know somebody must have done it
> already.
>
> i have a directory with 901 files in it.
> i need a script the renames the files. basically i need it to take
> anything that is not a number,letter, or "." and change it to an "_".
>
> ie. "Charlie's Root.zip" - will become "Charlie_s_Root.zip"
>
>
> perl/sh/bash, whatever, dosn't matter. :)

Assuming there is no leading/trailing whitespaces or newlines,
/bin/ls -d *[^a-zA-Z0-9_]* | while read i;
j=${i//[^a-zA-Z0-9_]/_}
mv "$i" "$j"
done

--
William Park
Open Geometry Consulting, Toronto, Canada