Unix Shell - rename multiple files in multiple directories

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2007 > rename multiple files in multiple directories





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author rename multiple files in multiple directories
p@p.com

2007-01-29, 7:23 pm

All,

I have a sol 10 server running samba and I have a varied directory structure
under /data that has loads of sub dir's with loads of *.gho files scattered
in this structure.

I need a script to find any "*.gho" files and change them to "*.aaa.gho"
files, I have managed to find/try the following with limited success

for f in *.gho ; do mv $f `basename $f gho`v0.1.gho; done
This does rename and gho files, but only does it for the PWD, while (how can
I repeat this for every sub dir ?)

find * - type f -print | grep gho
finds all gho files in all sub dirs ( how can I then rename every gho file
it finds ?).

Somehow I need to stitch the two together, your help is appreciated.....

(I do not want to use perl, I'm after as simple(for me to understand) as
possible script)


Thanks

again

Paul


Xicheng Jia

2007-01-29, 7:23 pm

On Jan 29, 4:09 pm, "p...@p.com" <P...@p.com> wrote:
> All,
>
> I have a sol 10 server running samba and I have a varied directory structure
> under /data that has loads of sub dir's with loads of *.gho files scattered
> in this structure.
>
> I need a script to find any "*.gho" files and change them to "*.aaa.gho"
> files, I have managed to find/try the following with limited success
>
> for f in *.gho ; do mv $f `basename $f gho`v0.1.gho; done
> This does rename and gho files, but only does it for the PWD, while (how can
> I repeat this for every sub dir ?)
>
> find * - type f -print | grep gho
> finds all gho files in all sub dirs ( how can I then rename every gho file
> it finds ?).
>
> Somehow I need to stitch the two together, your help is appreciated.....
>


Under bash (dont know if it works under sol 10 though):

find * -type f -name "*.gho" -print | while IFS=; read -r f; do mv
"$f" "${f%.gho}.aaa.gho"; done

(assumed no newline in filenames)

Regards,
Xicheng

p@p.com

2007-01-29, 7:23 pm

Xicheng Thanks,

Just tested it and thats perfect, thank you, I would like to understand it a
bit more though...

Regards
Paul

"Xicheng Jia" <xicheng@gmail.com> wrote in message
news:1170109761.637408.164200@a75g2000cwd.googlegroups.com...
> On Jan 29, 4:09 pm, "p...@p.com" <P...@p.com> wrote:
>
> Under bash (dont know if it works under sol 10 though):
>
> find * -type f -name "*.gho" -print | while IFS=; read -r f; do mv
> "$f" "${f%.gho}.aaa.gho"; done
>
> (assumed no newline in filenames)
>
> Regards,
> Xicheng
>



Steffen Schuler

2007-01-29, 7:23 pm

p@p.com wrote:
> All,
>
> I have a sol 10 server running samba and I have a varied directory structure
> under /data that has loads of sub dir's with loads of *.gho files scattered
> in this structure.
>
> I need a script to find any "*.gho" files and change them to "*.aaa.gho"
> files, I have managed to find/try the following with limited success
>
> for f in *.gho ; do mv $f `basename $f gho`v0.1.gho; done
> This does rename and gho files, but only does it for the PWD, while (how can
> I repeat this for every sub dir ?)
>
> find * - type f -print | grep gho
> finds all gho files in all sub dirs ( how can I then rename every gho file
> it finds ?).
>
> Somehow I need to stitch the two together, your help is appreciated.....
>
> (I do not want to use perl, I'm after as simple(for me to understand) as
> possible script)
>
>
> Thanks
>
> again
>
> Paul
>
>

with GNU toolchain:

find * -type f -name "*.gho" -print0 |
xargs -r0 -I {} bash -c 'f="{}"; mv "$f" "${f%gho}aaa.gho"'


This works also for newlines and other whitespace in your filenames.
It's tested.

Instead of an explanation, look for:

info find
man 1 xargs
man 1 bash

Enjoy,

Steffen
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com