LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-02-2009, 05:45 AM   #1
sahil.jammu
Member
 
Registered: Jun 2008
Distribution: Ubuntu
Posts: 83

Rep: Reputation: 15
Sed Command - - -How to change part of the name of multiple files


Hi Friends,

I have a doubt, as sed command is a stream editor, use to perform edits on stream of data. Given a file name to process sed will perform search and replace on all lines of file and sending modified data to standard output.

What if we want to replace multiple files together. For example i have a directory which contains 100 files and i want to replace many of them starting with particular string and i want extension to be same.

Example :-

dksdlsdklskdlsdklslssdsll.txt
ab123_12fhfhfkfkhd.jdslds.txt
ab123_12ssksksdksk.jdksdk.avi
ab123_12jskskjdjks.kdkddk.txt

sdjsdjksksddjksdjs.jsjdsk.txt
sdsdksdsdjsdksdkskskskssk.txt


I want to change ab123_12 with some string say hello , how can i do this with sed.

Kindly provide your inputs.

Many thanks in advance.

Regards,
Sahil
 
Old 04-02-2009, 05:53 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
You should never ask questions and try to demand a certain tool to be used etc. this is not what sed is for in the main. Here sed can be used to do a string substituion, but that's only a tiny part fo the overall problem.... the sed part would just be something like "sed s/abc_12/hello/". but you also need to iterate through a list, pass each item through sed to obtain the new name, and then run a rename from the original name to the new name. very little of that is sed.

Code:
for ORIG in *
do
  NEW=$(echo $ORIG | sed s/abc_12/hello/)
  [ $ORIG != $NEW ] && mv $ORIG $NEW
done
should work.
 
Old 04-02-2009, 06:00 AM   #3
Vit77
Member
 
Registered: Jun 2008
Location: Toronto, Canada
Distribution: SuSE, RHEL, Mageia
Posts: 132

Rep: Reputation: 17
Why not to use just this:
Code:
rename ab123_12 hello *
without sed?
 
Old 04-02-2009, 06:29 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
how did I not know that? seriously... how?

Certainly adds more weight to not demanding certain types of solution. Unless it's homework of course...

Last edited by acid_kewpie; 04-02-2009 at 06:30 AM.
 
Old 04-02-2009, 06:34 AM   #5
sahil.jammu
Member
 
Registered: Jun 2008
Distribution: Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks Chris for the reply.
If not by sed , what you suggest can be the way in to get the desired output.

Thnx!
Sahil
 
Old 04-02-2009, 06:56 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
did you not read the rest of the replies?!?!
 
Old 04-02-2009, 10:57 PM   #7
sahil.jammu
Member
 
Registered: Jun 2008
Distribution: Ubuntu
Posts: 83

Original Poster
Rep: Reputation: 15
Hi Chris

I read your first reply and its working fine for me. Thanks.
 
  


Reply



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
combining multiple sed operations into a single command kushalkoolwal Programming 3 09-16-2008 05:58 PM
how do I write a script using sed to delete the first and last line in multiple files Rikki D Programming 4 05-02-2008 11:01 PM
TAR command to unpack files to NTFS part idamien Linux - Newbie 4 05-22-2007 11:50 AM
Changing multiple files with SED timmy01 Linux - Software 17 05-15-2007 08:10 AM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 08:16 PM

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

All times are GMT -5. The time now is 09:42 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
Open Source Consulting | Domain Registration