How to change a file's owner and group in Linux

In this post, we will look at how the change the owner, and group ownership of files and directories using the chown and chgrp commands.



You will need a basic understanding of ownership and permissions in order to use this guide.

If you need to change the owner, or group of a file, folder, or the entire contents of a folder then you should be using the chown command using a tool such as PuTTy.putty.png

Please note, you can only carry out these actions if you are logged into PuTTy as the Root user.

Set a file's owner:

$ chown username filename

Once you have input this commend, the file "filename" will be changed to the new user "username".

You can also set a files group and owner in the same command:

$ chown username:usergroup filename

After giving this command, the file "filename" will belong to the user "username" and the group "usergroup"

This command works the same if you enter a filename or a directory name:

$ chown username:usergroup /var/directory/path

Once you have entered this command, the owner and the group of the directory "path" will be changed.

Please note however, that this will only change the owner of the file or folder - NOT all files recurively.

In order to change the owner of all files in a directory, we would use the "Recursive" command -R

$ chown -R username /var/directory/path

In the above command, we are making the user "username" the owner of all files in the folder "path" which is located in /var/directory/

If you want to be shown a list of what you have done, then you will need to use the verbose command -v.

$ chown -v username filename

changed ownership of 'filename' to username

This command is useful if you are changing the ownership of several files at once, and you require confirmation

Another example of this would be as follows, where we change the owner of a directory, and use the verbose command to tell us what has changed:

$ chown -Rv username /var/directory/folder

changed ownership of 'folder/' to username
changed ownership of 'folder/boringfile' to username
changed ownership of 'folder/somefile' to username


Finally, we will look in more detail at changing the grow ownership of a file.

As well as chown, as shown above, there is an additional command that you can use called chgrp which allows you to change the group ownership of a file of directory.

Again, I will be using PuTTy to carry this out, and again you must be logged in as Root to change group ownership.

chgrp works in almost the same way as chown does, exect that it doesn't change the user, but the group of a file..

$ chgrp usergroup filename

On inputting this command, the file "filename" will be owned by the group "usergroup". However, the owner (user) of the file will remain the same.

The options for using chgrp are exactly the same as when using chown. So, for example, the -R and -v options will work with with chgrp just as they do with chown:

$ chgrp -Rv usergroup /var/directory/folder
changed group of '/var/directory/folder' to usergroup
changed group of '/var/directory/folder/filename1' to usergroup
changed group of '/var/directory/folder/filename2' to usergroup

 

About Gecko Social

We're Gecko Social; we are Social Web Experts based in Manchester.

 

We make it easy for people to use the Social Web to grow their business by providing a range of Social Web Services such as Marketing, Web Design and Consultancy.

 

We can help with Web Design & Development, Online & Search Engine Marketing, Social Media and Web Management.