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
addadyAsked:
Who is Participating?
 
xDamoxConnect With a Mentor Commented:
Hi,

I am not 100% sure what you want to rename it to bu you could try:

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

Of course you will want to replace /home/damian/test with the path you need.
0
 
addadyAuthor Commented:
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
0
 
addadyAuthor Commented:
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.
0
 
addadyAuthor Commented:
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
0
 
xDamoxCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.