Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Linux replace all instances of a file

Posted on 2013-11-06
Medium Priority
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?

Question by:kelvan
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
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)
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)
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

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.


Author Comment

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.

Author Comment

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
LVL 35

Accepted Solution

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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