• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

Linux replace all instances of a file

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
kelvan
Asked:
kelvan
  • 4
  • 2
1 Solution
 
Duncan RoeSoftware DeveloperCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
You'd have to run it as root, so test all the component parts first (especially the find or locate)
0
 
Duncan RoeSoftware DeveloperCommented:
Oops! Forgot the do
for i in $(locate file1.php) ; do cat newfile > $i; done

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
kelvanAuthor Commented:
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
 
kelvanAuthor Commented:
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
 
Duncan RoeSoftware DeveloperCommented:
The prior solution did retain permissions and owners. That is the whole reason to use cat to overwrite existing files.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now