start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 22.8 The Handy chmod = Operator Chapter 22
File Security, Ownership, and Sharing
Next: 22.10 cx, cw, c-w: Quick File Permission Changes
 

22.9 Protect Important Files: Make Them Unwritable

A good way to prevent yourself from making mistakes is to make certain files read-only. If you try to delete a read-only file, you will get a warning. You will also get a warning if you try to move a file onto another file that is write - protected. If you know you want to remove or move a file, even though the file is read-only, you can use the -f option with rm or mv to force the change without warnings.

Manually changing the permissions of files all the time is counterproductive. You could create two aliases to make it easier to type:


# change mode to read only 
alias -w chmod -w
# change mode to add write permission
alias +w chmod u+w

[These are really handy! I use a script named c-w and cw, respectively, instead. For shell programming, I also added cx that does chmod +x. Article 22.10 explains the script. -JP ] It is a good idea to remove write permission from some files. Occasionally some files contain information difficult to replace. These files might be included with other, easily replaceable files. Or you might want to protect some files that rarely change. Combined with directory permissions, and the current value of umask (22.4), you can find some file that might be protected in this manner. You can always create a script that adds write permission, edits the file, and removes write permission:



"$@" 


${..=..} 


#!/bin/sh
# add write permission to the files

chmod u+w "$@"
# edit the files; use vi if VISUAL not defined
${VISUAL=vi} "$@"
# remove write permission
chmod -w "$@"

- BB


Previous: 22.8 The Handy chmod = Operator UNIX Power ToolsNext: 22.10 cx, cw, c-w: Quick File Permission Changes
22.8 The Handy chmod = Operator Book Index22.10 cx, cw, c-w: Quick File Permission Changes

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System