Link to home
Start Free TrialLog in
Avatar of addady
addady

asked on

Rename some of the files in a directory tree

Hi,

I would like to rename on file ending with *.*_old to *.*

It work on the current directory well using:
for a in *_old; do mv "$a" "$(echo "$a" | sed 's/_old//')";

How do I expand it so that it will rename all the file in the subdirectories?

Regards,
Addady
ASKER CERTIFIED SOLUTION
Avatar of xDamox
xDamox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of addady
addady

ASKER

Hi,

Thank you it works. you hace small syntax errors. Here is the rigth command:

for dir in `find /home/damian/test  -iname \*_old`; do mv $dir `echo $dir | sed -e 's/_old//'`; done

Its not work.  For example:

#find /tmp -iname \*.html
/tmp/polls.html
/tmp/activejobs.html
/tmp/2del/polls.html

#for dir in `find /tmp -type d -iname \*.html`; do mv $dir `echo $dir | sed -e 's/.html/.html-old/g'`; done
Avatar of addady

ASKER

I'm still have problem with space in file name.  the mv command is break :(

Change it to:

find /home/damian/test  -iname \*_old`; do mv "$dir" `echo "$dir" | sed -e 's/_old//'`; done
 or
find /home/damian/test  -iname \*_old`; do mv \"$dir\" `echo \"$dir\" | sed -e 's/_old//'`; done
didn't solve it.
Avatar of addady

ASKER

Let me be more spcific:

#find /tmp/ -iname \*.genc__12-24__26-07-2007                
/tmp/051008 - Eilat/P1010039.JPG.genc__12-24__26-07-2007
/tmp/051008 - Eilat/P1010037.JPG.genc__12-24__26-07-2007
/tmp/051008 - Eilat/P1010036.JPG.genc__12-24__26-07-2007

The directory name contain space

#for FILE in "`find /tmp/ -iname \*.genc__12-24__26-07-2007`"; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done


fail because of the space in the directory name.
Please provide example.

Thanks
Hi,

First at the command line type

IFS="
"

Notice after the quotes I hit enter then on that new I added the closing quote now the command

for FILE in "`find /tmp/ -iname \*.genc__12-24__26-07-2007`"; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done

Should work fine.