Solved

Linux replace all instances of a file

Posted on 2013-11-06
6
287 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SonarQube on Linux vs Windows 3 52
Upgraded from Debian 7 to 8.7 and got black screen 20 69
Syslog-ng works. Now what? How to filter and manage? 8 64
BATCH to EXE Converter 2 39
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 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