LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2011, 12:22 PM   #1
ppat18
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Rep: Reputation: 0
Rename a file in batch of directories in linux


Hi all,

I am newbie in scripting and have a task. I want to rename a file in batch of directories (about 2000 directories) in linux. possibly a bash or perl script.

I have a file called sms in each directory (all 2000 directories) which I need to rename it to sms.old

so needs to look like this:

/home/aa/AA/sms to /home/aa/AA/sms.old
/home/bb/BB/sms to /home/bb/BB/sms.old
..
..

Any help would be greatly appreciated.

Thanks
 
Old 11-15-2011, 01:27 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,481

Rep: Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089
Like this?

Code:
for i in /home/*/*/sms; do
   echo mv $i $i.old
done
Run it like this to make sure the move commands are exactly what you want, and if so, remove the "echo" and run it again to actually do the task, otherwise modify the script to suit and test it again.
 
1 members found this post helpful.
Old 11-15-2011, 01:51 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Renaming files is a very common scripting question, and has been answered often. Search the forum a bit and you should find all the answers you need. (Hint, start with the "Similar Threads" box below.)

There are also a number of ready-made renaming utilities you can use. Search the web or your software repositories.

Last edited by David the H.; 11-15-2011 at 01:52 PM.
 
1 members found this post helpful.
Old 11-15-2011, 02:15 PM   #4
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by suicidaleggroll View Post
Like this?

Code:
for i in /home/*/*/sms; do
   echo mv $i $i.old
done
Run it like this to make sure the move commands are exactly what you want, and if so, remove the "echo" and run it again to actually do the task, otherwise modify the script to suit and test it again.
I think this will only find file that are exactly two directories deep ... I would use find
Code:
find /home -name sms -exec echo mv "{}" "{}.old" \;
 
1 members found this post helpful.
Old 11-15-2011, 02:45 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,481

Rep: Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089Reputation: 2089
Quote:
Originally Posted by SecretCode View Post
I think this will only find file that are exactly two directories deep ... I would use find
Code:
find /home -name sms -exec echo mv "{}" "{}.old" \;
Correct, it's all a matter of what the OP needs. Sometimes hardcoding it to a fixed depth can be safer, assuming the files he wants to rename are all at the same depth.
 
1 members found this post helpful.
Old 11-15-2011, 06:19 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
For extra safety you might add
Code:
-type f
as well ...
 
1 members found this post helpful.
Old 11-17-2011, 03:09 PM   #7
ppat18
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by SecretCode View Post
I think this will only find file that are exactly two directories deep ... I would use find
Code:
find /home -name sms -exec echo mv "{}" "{}.old" \;
Awesome!! the find worked. I used it with -type f in a bash script and logged it.

thank you guys for making this so much easier!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch rename files samnjugu Linux - Desktop 7 06-02-2009 10:06 AM
Batch file rename assistance please OstermanA Linux - General 10 10-20-2008 01:25 PM
Batch Rename shelfitz Linux - Newbie 19 10-12-2006 10:23 PM
Bash - Batch File Rename Help... emailarron Linux - Newbie 4 01-26-2006 07:35 AM
Batch rename question hellblade Linux - Software 4 05-03-2004 03:57 PM


All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration