Solved

shell find and replace

Posted on 2013-06-14
3
511 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 77

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 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