Thursday, November 27, 2008

chown

Setting File Ownership with chown


Problem
You need to change ownership on a file or directory. Perhaps you've copied something to someone else's directory but she still can't edit it, because it's owned by you. Many Linux problems result from incorrect ownership or permission. You're seeing messages like "Permission denied" or "File is read-only" when it shouldn't be.


Solution
Use chown (change owner) to change the file owner, the file and group owner, or the group owner:
# chown user filename
# chown user:group filename
# chown :group filename

For example:
$ chown -v carlas:share index.txt
changed ownership of `index.txt' to carlas:share
$ chown -v :share toc.txt
changed ownership of `toc.txt' to :share


Problem
You want to change ownership of directories and their contents, or just the contents of directories, a list of files, or change ownership of files from one UID to another.


Solution

chown supports some batch operations, or you can use find, or you can use shell wildcards.

you can use shell wildcards:
# chown carlas *.txt

To give all of a user's files to another user, use:
# chown -R -v from valh piglet /shared/scripts

You can do the same thing with find:
# find / -user valh -exec chown -v piglet { } \;

find can also search by UID, which chown cannot:
# find / -uid 1050 -exec chown -v 1200 { } \;

To change the ownership of a directory, including subdirectories and files, with verbose output, use:
# chown -R -v piglet /shared/scripts

changed ownership of `scripts' to piglet
changed ownership of `scripts/backups.tgz' to piglet
changed ownership of `scripts/fake-spec-rpm' to piglet

Either the user's login name or UID can be used. If you've deleted a user and the user has left behind orphan files, you'll need the UID.

No comments:

Post a Comment