Solved

Linux replace all instances of a file

Posted on 2013-11-06
6
277 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
  • 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
This article will show, step by step, how to integrate R code into a R Sweave document
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 …
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now