O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Locking a file from changes
Mac OS X keep overwriting your hand changes to files? Read on to find out how to lock the file.

Contributed by:
Jason Deraleau
[07/16/03 | Discuss (2) | Link to this hack]

Mac OS X and Mac OS X Server come with several GUI tools to make configuring your Mac easier. Many of these tools make changes to the configuration files of the various daemons on your system. However, these tools can often become confused when you make your own changes to the files by hand. They might overwrite your changes or fail to load in your custom settings.

While there's not much one can do to get such a tool to recognize your settings, there is a utility which can keep them from being overwritten. This utility is called chflags and it lives in the /usr/bin folder. The chflags tool both locks and unlocks a file for changes. Once a file is locked, it cannot be modified at all. Not by another utility and not by a user.

To lock a file, issue the command chflags uchg filename. If you are modifying a system file, you will need to have administrator access [See Mac OS X Hack #50] to use chflags. Now that the file is locked, try using touch or an editor to modify the file. You'll get a permission denied error.

Now, to unlock the file, use the command chflags nouchg filename. You should now be able to modify the file once again. Just remember to lock it back up again, or you might get your changes overwritten.

See also: man chflags

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.