Solved

shell find and replace

Posted on 2013-06-14
3
525 Views
Last Modified: 2013-06-15
Hi,

I am trying to to a seemingly easy task but seem to do it wrong. I wonder if someone can give me some assistance on this.

I simply like to find a file and replace it. I looked in google and there are several examples but it seems they are only concerned to rename extensions so somehow I can not make it work.

What I try to do is to rename the file ".something" to ".anything" across a specified directory and the sup directories within it.

I would be very happy if someone could assist me on what command to us. I run CentOS 6 Linux.

Best wishes,
thom
0
Comment
Question by:Thomanji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 79

Assisted Solution

by:arnold
arnold earned 200 total points
ID: 39249513
Find /path -name "*.something" | grep "somedirectory" | while read a; do
b=^' echo $a | sed -e 's/(^.*)\.something$/$1.anything/' ^'
Echo "mv $a $b"
Done
0
 
LVL 48

Accepted Solution

by:
Tintin earned 300 total points
ID: 39249630
for file in $(find /start/dir -type f -name "*.something")
do
  mv $file ${file%%.something}.anything
done

Open in new window

0
 

Author Closing Comment

by:Thomanji
ID: 39249921
Thank you both. Both solutions work fine but I like the simplicity of the second.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

635 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