Solved

shell find and replace

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
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.:

828 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