How to change ownership of files/folders in Cent OS/linux?
Posted: Thu Mar 22, 2012 1:45 am
You can change the owner and group of a file or a directory with the chown command. Please, keep in mind you can do this only if you are the root user or the owner of the file.
Set the file's owner:
After giving this command, the new owner of a file called somefile will be the user username. The file's group owner will not change. Instead of a user name, you can also give the user's numeric ID here if you want.
You can also set the file's group at the same time. If the user name is followed by a colon and a group name, the file's group will be changed as well.
After giving this command, somefile's new owner would be user username and the group usergroup.
You can set the owner of a directory exactly the same way you set the owner of a file:
Note that after giving this command, only the owner of the directory will change. The owner of the files inside of the directory won't change.
In order to set the ownership of a directory and all the files in that directory, you'll need the -R option:
Here, R stands for recursive because this command will recursively change the ownership of directories and their contents. After issuing this example command, the user username will be the owner of the directory somedir, as well as every file in that directory.
Tell what happens:
changed ownership of 'somefile' to username
Here, v stands for verbose. If you use the -v option, chown will list what it did (or didn't do) to the file.
The verbose mode is especially useful if you change the ownership of several files at once. For example, this could happen when you do it recursively:
changed ownership of 'somedir/' to username
changed ownership of 'somedir/boringfile' to username
changed ownership of 'somedir/somefile' to username
As you can see, chown nicely reports to you what it did to each file.
Changing the group ownership of a file
In addition to chown, you can also use the chgrp command to change the group of a file or a directory. You must, again, be either the root user or the owner of the file in order to change the group ownership.
chgrp works pretty much the same way as chown does, except it changes the file's user group instead of the owner, of course.
After issuing this command, the file somefile will be owned by a user group usergroup. Although the file's group has changed to usergroup, the file's owner will still be the same.
The options of using chgrp are the same as using chown. So, for example, the -R and -v options will work with it just like they worked with chown:
changed group of 'somedir/' to usergroup
changed group of 'somedir/boringfile' to usergroup
changed group of 'somedir/somefile' to usergroup
Set the file's owner:
Code: Select all
$ chown username somefile
You can also set the file's group at the same time. If the user name is followed by a colon and a group name, the file's group will be changed as well.
Code: Select all
$ chown username:usergroup somefile
You can set the owner of a directory exactly the same way you set the owner of a file:
Code: Select all
$ chown username somedir
In order to set the ownership of a directory and all the files in that directory, you'll need the -R option:
Code: Select all
$ chown -R username somedir
Tell what happens:
Code: Select all
$ chown -v username somefile
Here, v stands for verbose. If you use the -v option, chown will list what it did (or didn't do) to the file.
The verbose mode is especially useful if you change the ownership of several files at once. For example, this could happen when you do it recursively:
Code: Select all
$ chown -Rv username somedir
changed ownership of 'somedir/boringfile' to username
changed ownership of 'somedir/somefile' to username
As you can see, chown nicely reports to you what it did to each file.
Changing the group ownership of a file
In addition to chown, you can also use the chgrp command to change the group of a file or a directory. You must, again, be either the root user or the owner of the file in order to change the group ownership.
chgrp works pretty much the same way as chown does, except it changes the file's user group instead of the owner, of course.
Code: Select all
$ chgrp usergroup somefile
The options of using chgrp are the same as using chown. So, for example, the -R and -v options will work with it just like they worked with chown:
Code: Select all
$ chgrp -Rv usergroup somedir
changed group of 'somedir/boringfile' to usergroup
changed group of 'somedir/somefile' to usergroup