Unix Power ToolsUnix Power ToolsSearch this book

50.7. 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:

Figure Go to http://examples.oreilly.com/upt3 for more information on: chmod.csh, chmod.sh

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

Figure Go to http://examples.oreilly.com/upt3 for more information on: chmod_edit

[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. Section 50.8 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 (Section 49.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:

"$@"Section 35.20, ${..=..}Section 36.7

#!/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



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.