Another commonly used linux command is the chown command. ‘chown‘ is short for change owner. It allows users to change the owner and group associated with a file.
Synopsis: chown [OPTION]… [OWNER][:[GROUP]] FILE…
|Option||Long option name||Description|
|-c||–changes||Operates like verbose (-v) but report only when a change is made.|
|-h||–no-dereference||affect each symbolic link instead of any referenced file (useful only on systems that can change the ownership of a symlink).|
|-v||–verbose||Outputs a diagnostic for every file processed.|
|-f||–silent –quiet||Suppresses most error messages.|
|-R||–recursive||Operate on files and directories recursively.|
Examples using chown.
For the following examples, assume the file test.txt starts out with only ‘Read’ permissions (r) for all three groups (user, group, and other). It will appear as the following:
# ls -l test.txt -r--r--r-- 1 root root 29K Jun 17 12:01 test.txt
Changing the owner of a file
chown [OWNER] FILE… – This is one of the most basic ways to use the command. In this example, we simply change the test.txt file, which is originally owned by the root user, to be owned by the user named test.
# chown test test.txt # ls -l -rw-r--r-- 1 test root 29K Jun 17 12:01 test.txt
Changing the group of a file
chown [:GROUP] FILE… – Much like the previous example, we are using no options. We will be changing just the group of the file to test. It is almost the same syntax except you must precede the group name with a colon :.
# chown :test test.txt # ls -l -rw-rw-rw- 1 root test 29K Jun 17 12:01 test.txt
Changing both owner and group at once.
chown [OWNER][:[GROUP]] FILE… – Combining the method of the previous two examples, we will change the owner and group of the test.txt file from root to test.
# chown test:test test.txt # ls -l test.txt -rw-r-xr-- 1 test test 29433 Jun 17 12:01 test.txt
Using the verbose option.
chown [OPTION]… [OWNER][:[GROUP]] FILE…. – We now will demonstrate one of the most common options, -v, or verbose. This will display the changes that were made as the command is run. As you can see, we do not have to use the ‘ls‘ command afterwards in order to see how the permissions ended up.
# chown -v test:test test.txt changed ownership of `test.txt' to test:test
Changing the ownership/group of a directory.
chown [OWNER][:[GROUP]] [DIR] – Working with directories is very much the same as a file. Below we will be changing the image directory to use the owner and group named test. The only thing different is that we name a directory instead of a file.
# chown test:test images # ls -l drwxr-xr-x 2 root root 4096 Jun 26 14:13 images/
Change all files in a directory recursively
chown [OPTION]… [OWNER][:[GROUP]] [DIR] – If you need all files within a directory changed to the same owner or group, you will want to use the -R, or recursive option. This saves time over doing each one separately. Be warned that this will change all files and directories within the named directory including the named directory.
# chown -R test:test images # ls -l drwxr-xr-x 2 test test 4096 Jun 26 14:13 ./ drwxr-xr-x 3 root root 4096 Jun 26 14:04 ../ -rw-r--r-- 1 test test 0 Jun 26 14:13 file1.png -rw-r--r-- 1 test test 0 Jun 26 14:13 file2.png -rw-r--r-- 1 test test 0 Jun 26 14:13 file3.png