Solved

shell find and replace

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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 …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now