Skip to main content
How To

How to Change File Ownership in Ubuntu

Here's how to change the ownership of files and folders in Ubuntu command line.

Sagar Sharma

From its core, Linux is built to have multiple users. Be it a root user and a normal user or a bunch of human users.

But what if you want to access files created by other useres? Well, here's what it looks when you try accessing a file owned by another user:

user is not allowed to access file

While you can use the ls command to check file permissions, I will be using the getfacl utility as it presents the output in a better way compared to the others:

check file ownership in linux

Don't worry. You don't need to change the user to access a file owned by another user.

Let me show you how to change ownership of files so no one can access them unless it is you!

Change file owner using the chown command

This guide will utilize the chown command as it is intended to deal with file and group ownership. So let's have a look at the syntax first:

chown [OPTIONS] USER[:GROUP] file

Here,

  • [OPTIONS] allows you to tweak the default behavior of chown.
  • [USER] is where you'll specify the new owner using username or user-id.
  • [:GROUP] is optional and should only be used when you want to change the group ownership of a file.
  • file is where you have to a filename of which you want to change ownership.

So now, let's have a look at how you can change file ownership.

1. Change file ownership

To shift ownership of a file to a new user, you just have to append the filename with the new user:

sudo chown NewOwner File

For example, I'll be changing ownership of Music.mp3 to the user named Milan:

sudo chown milan Music.mp3
change ownership of file in ubuntu

As you can clearly see, the owner was changed to milan after executing shown command.

2. Change ownership of multiple files and directories

To change ownership of multiple files, you just have to chain the filenames with a new owner:

sudo chown NewOwner File1 File2 File3
change ownership of multiple files in ubuntu

Similarly, you can combine the directory names with filenames and change their owner at once:

sudo chown NewOwner Dir1 File1 Dir2 File2
change ownership of files and directories in ubuntu

3. Change the group of file

As you can clearly see, the group users have their own privileges, and if you want to change the group of files, specify the group name with a colon (:) as shown:

sudo chown :NewGroup File

For example, I will be changing the group of a file named Music.mp3 to LU:

sudo chown :LU Music.mp3
change group of file

4. Change file ownership and group at once

To change the file owner and file group, you have to append the group name using a colon (:) to the new user:

sudo chown NewOwner:NewGroup File

For example, I will be changing the owner to milan and the file group to LU of the Music.mp3 file:

sudo chown milan:LU Music.mp3
change file owner and file group at once in linux
How to Remove User from Group in Ubuntu Command Line
Added a user to the wrong group? You can remove the user from a given group using either the deluser or usermod commands.

5. Change file ownership and group recursively

To change the file owner and group recursively, you'd need to utilize the -R option and append the directory or path of the directory and the changes will be applied to every file and subdirectory.

To change only the file ownership recursively:

sudo chown -R NewOwner NameOfDirectory

Let's say I want to change the owner of every file present inside the directory name Directory, so I'd have to follow the given command:

sudo chown -R milan Directory
change file user recursively

Similarly, you can change the ownership and group at once by following the given command syntax:

sudo chown -R NewOwner:NewGroup DirectoryName

For example, I will be changing ownership to a user named milan and change the group to LU to every file present inside the Directory:

sudo chown -R milan:LU Directory
change file ownership and group recursively in linux

6. Change file ownership and group with the help of reference file

Let's say you have one file with desired owner and group already configured and now you want to apply those same changes to other files.

This can be done by appending the reference file to the --reference option:

sudo chown --reference=Reference_File TargetedFile

I have a reference file named PerfectFile.txt and I want to replicate the owner and group to the file named Music.mp3:

sudo chown --reference=PerfectFile.txt Music.mp3
change file ownership and group with the help of reference file

Wrapping Up

This was my take on how you can change ownership of files and groups with different options involved.

Sagar Sharma