home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

50.15. How to Change File Ownership Without chown

Unix systems with disk quotas (Section 15.11) won't let you change the owner (Section 50.14) of a file; only the superuser can use chown. Here's a workaround for those systems.

-d Section 8.5, -f Section 14.10

  1. The file's current owner should make sure that the new owner has write permission on the directory where the file is and read permission on the file itself:

    jerry% ls -dl . afile
    drwxr-xr-x   2 jerry    512  Aug 10 12:20 .
    -rw-r--r--   1 jerry   1934  Aug 10 09:34 afile
    jerry% chmod go+w .
  2. The new owner (logged in as herself) should rename the file, make a copy, and delete the original file. If the new owner is there at the same time, su (Section 49.9) is probably the fastest way to change accounts:

    jerry% su laura
    Password:
    laura% mv afile afile.tmp
    laura% cp -p afile.tmp afile
    laura% ls -l afile
    -rw-r--r--   1 laura   1934  Aug 10 09:34 afile
    laura% rm -f afile.tmp
    laura% exit
    jerry% chmod go-w .

The cp -p (Section 10.12) command preserves the file's original permissions and last modification time. After the new owner (laura) is done copying, the old owner (jerry) takes away the directory's write permission again. Now laura can edit afile, change its modes, and so on: she owns it.

-- JP



Library Navigation Links

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