You are not logged in.

#1 2007-11-12 03:24:53

_adam_
Member
From: Dora, Alabama
Registered: 2006-05-18
Posts: 94

problem with adding prefixes to files in bash

i'm trying to write a script that will copy files from all directories below the pwd into the pwd and add a prefix of "[directory name]-" so that identically named files will not be overwritten and so i can still determine what group they belong to. doing something like:
for i in *; do for h in $i/*; mv $h ./$i-$h; done; done;
is obviously gonna be messed up because $h will expand to $directory/$file.

can anyone point me in the right direction to do this?

Offline

#2 2007-11-12 03:35:38

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: problem with adding prefixes to files in bash

Try this:

for file in $(find -type f); do mv "$file" "$(echo $file | sed 's#^\.\(.*\)/#[\1]-#;s#/##g')"; done

Last edited by Gilneas (2007-11-12 03:39:21)

Offline

#3 2007-11-12 04:19:43

_adam_
Member
From: Dora, Alabama
Registered: 2006-05-18
Posts: 94

Re: problem with adding prefixes to files in bash

THANKS!

now i need to brush up on my sed skills and figure out whats happening there. thanks again.

Offline

#4 2007-11-12 12:40:07

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: problem with adding prefixes to files in bash

Also, basename is your friend

for i in *; do for h in ${i}/*; mv ${h} ./${i}-$(basename ${h}); done; done;

Offline

Board footer

Powered by FluxBB