Solved

shell find and replace

Posted on 2013-06-14
3
503 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
3 Comments
 
LVL 76

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now