Solved

Linux replace all instances of a file

Posted on 2013-11-06
6
290 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 34

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 34

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 34

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 34

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

734 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