?
Solved

Rename some of the files in a directory tree

Posted on 2007-08-01
5
Medium Priority
?
477 Views
Last Modified: 2010-04-20
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
0
Comment
Question by:addady
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
xDamox earned 200 total points
ID: 19607540
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
 

Author Comment

by:addady
ID: 19607829
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
 

Author Comment

by:addady
ID: 19607924
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
 

Author Comment

by:addady
ID: 19610573
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
 
LVL 16

Expert Comment

by:xDamox
ID: 19610701
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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question