LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-10-2015, 07:29 PM   #1
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Rep: Reputation: 37
sed use to rename files under multiple directory


Hi all,

I am trying to use sed to rename multiple files under multiple directory.

so lets say for exmaple:
Under /root I have 2 directory as follows:

# ll {test1,test2}
test1:
total 0
-rw-r--r--. 1 root root 0 Apr 10 19:16 authkey.apollo

test2:
total 0
-rw-r--r--. 1 root root 0 Apr 10 19:16 authkeys.apollo

if I want to change apollo to jupiter then used this:
for i in `ls {test1,test2} | grep -i 'apollo'`; do echo $i; sed -i 's/apollo/jupiter/g'; done

but it seems like it got missed on the file path in sed. is there any other easier way or better approach to make this work?

Thanks in advance.
 
Old 04-10-2015, 07:42 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,855

Rep: Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760
sed can change the content of a file, but not its name. Try mv or rename.
 
Old 04-10-2015, 09:05 PM   #3
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 37
Thanks for the reply.

will look into it
 
Old 04-10-2015, 09:21 PM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,678

Rep: Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566Reputation: 1566
It is not considered good practice to parse the output of the 'ls' command when scripting.
In a bash shell, you can use parameter expansion rather than 'sed'
Code:
for f in test[12]/*.apollo; do mv "$f" "${f/apollo/jupiter}"; done
 
Old 04-11-2015, 02:30 AM   #5
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,855

Rep: Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760Reputation: 2760
You could also write this, longer but more portable as it doesn't use any bash extension to the Shell Command Language:
Code:
for i in $(find /root/test{1,2} -name "*.apollo"); do
    mv $i ${i%.apollo}.jupiter
done
 
Old 04-11-2015, 08:12 AM   #6
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 37
Thanks a lot for the replies . will change accordingly.

Also when changing inside files then how to omit the hidded folders from the search as the below brings also the hidden folders in search.

for i in `grep -lr apollo .`; do echo $i;cat $i | grep "apollo"; sed -s 's/apollo/jupiter/g' $i; done
output:
[root@tasnia ~]# for i in `grep -lr apollo .`; do echo $i;cat $i | grep "apollo"; sed -s 's/apollo/jupiter/g' $i; done
./.test
jupiter
./test
jupiter

Last edited by tanveer; 04-11-2015 at 08:20 AM.
 
Old 04-11-2015, 08:30 AM   #7
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 37
got it .. used grep -v
 
  


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
[SOLVED] BASH - Rename multiple files using tr and sed elalexluna83 Programming 6 04-25-2012 04:46 PM
Use sed to edit string in multiple files not contained in current directory DylanB Linux - Newbie 1 04-07-2011 03:27 AM
Using Sed to Rename A Group of Files smithjb Programming 6 05-03-2009 10:27 PM
how to rename files using sed? seimour Linux - Newbie 1 05-02-2007 02:46 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 07:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:13 AM.

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