Operating System - HP-UX
1825944 Members
2715 Online
109690 Solutions
New Discussion

script that updates a file but in many directories

 
nick_154
New Member

script that updates a file but in many directories

Greetings,

i have a .txt file called 'Master' and 75 user directories each containing the same 'Master' .txt file.

When i make changes to this 'Master' file i then have to copy & paste it into each of these 75 user directories.

So, my question is:

is there a script that can do all of this for me? A script that looks for changes in the 'Master' file and, if it detects changes, updates the 'Master' file in the 75 user directories.

Thank you for your time,

Nick
6 REPLIES 6
Rodney Hills
Honored Contributor

Re: script that updates a file but in many directories

How about this-

#!/usr/bin/sh
cd /topdir
find . ! -newer /your/Master.txt -name Master.txt -print | xargs -n1 cp /your/Master.txt

HTH

-- Rod Hills
There be dragons...
Marvin Strong
Honored Contributor

Re: script that updates a file but in many directories

you could make one easy enough.

assuming the file name is the same in all users directories. And all the users directories fall under a certain path, that is separate from your master file. If not it would be a little more complex, but still possible.

I wouldn't worry about making something watch for changes, although you could add something in cron to do that.

a simple loop like to following should get you started.

for file in `find /home -name master.txt`
do
cp master $file
done
Mike Stroyan
Honored Contributor

Re: script that updates a file but in many directories

If you want all directories to see the same file contents you could just make links from those directories to a single file.

ln -s /master_dir/Master.txt /user1_dir
ln -s /master_dir/Master.txt /user2_dir
...

Then opening /user1_dir/Master.txt will really open /master_dir/Master.txt . Any change to that file will immediately be seen by every link. (The permissions come from the real file rather than the owner and permissions of the link. That means that all users would need to be able to open the single Master.txt file.)
Hai Nguyen_1
Honored Contributor

Re: script that updates a file but in many directories

I like Mike's idea. Very brilliant!

Hai
Francisco J. Soler
Honored Contributor

Re: script that updates a file but in many directories

Hi,

If all the Master.txt files are in the same file system, you can try,

ln /master_dir/Master.txt /user1_dir/Master.txt

link without -s option, so if you delete the master file, no problem, all are master, and all are the same.

Frank.
Linux?. Yes, of course.
A. Clay Stephenson
Acclaimed Contributor

Re: script that updates a file but in many directories

I can't believe that anyone would be dumb enough to create 75 copies of the same file. In that case, it would be far smarter to simply have one file. My reading of this is that you are making the same change to a file and you would like this change to be replicated but leave the remainder of each file intact so that local customizations are preserved. If that is the case then you are going to have to script a solution using sed, awk, and/or Perl.


If it ain't broke, I can fix that.