Solved

Linux replace all instances of a file

Posted on 2013-11-06
6
301 Views
Last Modified: 2013-11-10
How would you go about overwriting every instance of a file on a server, while maintaining owner and permissions?

For example: Say I have a file named --> file1.php

I want to overwrite every copy on my server of file1.php with my new copy. However, since I am using WHM/cPanel... Those files will all be under different user accounts. So I want to still maintain the same owners, and file permissions of the original file.

Any ideas? Assume it would have to be a shell script that retrieved the permissions of a file before it overwrote it?

Thanks
0
Comment
Question by:kelvan
[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
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39628604
Yes it'd be a shell script. The way it would work would be to replace the contents of each file using cat instead of cp
for i in $(locate file1.php) ; cat newfile > $i; done

Open in new window

Assumes you have installed the slocate package (for locate). Otherwise you could use say $(find / -name file1.php)
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39628609
You'd have to run it as root, so test all the component parts first (especially the find or locate)
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39628790
Oops! Forgot the do
for i in $(locate file1.php) ; do cat newfile > $i; done

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 1

Author Comment

by:kelvan
ID: 39637370
Yeah I actually went a different route with a script that created a script with chown ad chmod commands in it after overwriting the files.
0
 
LVL 1

Author Comment

by:kelvan
ID: 39637438
I've requested that this question be closed as follows:

Accepted answer: 0 points for kelvan's comment #a39637370

for the following reason:

Because the prior answer did not retain permissions and owners
0
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 500 total points
ID: 39637439
The prior solution did retain permissions and owners. That is the whole reason to use cat to overwrite existing files.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

627 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