Managing File Ownership

6 min read·Jan 1, 2025

In Unix-like operating systems, file ownership refers to the user and group that have control over a particular file or directory through the use of file permissions.

Together, ownership and permissions allow to improve the overall security of the system by enabling administrators and users to control who can access and modify each file and directory.

Checking file ownership

To display detailed information about a file, including its ownership, you can use the ls command with the -l flag as follows:

$ ls -l [file ...]

Where file ... is an optional list of paths to the files you want to check.

Note: For directories, you'll have to specify the path to the parent directory, as the ls command will otherwise list the content of the target directory.

Example

This command will display extended information about all the files and directories located in the current working directory:

$ ls -l
-rw-r--r--  1 john  developers   0 Apr 22 16:18 script.sh

Where:

  • john is the name of the user the files belong to.
  • developers is the name of the group the files belong to.

Changing the owner

To change the owner and optionally the group of files and directories, you can use the chown command as follows:

$ [sudo] chown owner[:group] file ...

Where:

  • sudo is optionally used to execute the chown command with superuser privileges to change the ownership of files that don't belong to the currently logged in user.
  • owner is the name of the new owner.
  • group is optionally the name of the new group.
  • file ... is a list of paths to the files and directories you want to change the ownership of.

Note: When not using the sudo command, the user must be part of the new group in order to be allowed to change it.

Example

Let's consider the following script.sh file that belong to the user named john and the group named developers:

$ ls -l
-rw-r--r--  1 john  developers   0 Apr 22 16:18 script.sh

Assuming that the currently logged in user is john, this command will transfer the ownership of the script.sh to the user named alice:

$ chown alice script.sh

This command will transfer the ownership of the file script.sh back to the user named john:

$ sudo chown john script.sh

Note: The use of the sudo command is required here since the file doesn't belong to the currently logged in user (i.e., john).

This command will transfer the ownership of the script.sh file to the user named jack and the group named testers:

$ chown jack:testers script.sh

Changing the group

To only change the group of files and directories, you can use the chgrp command as follows:

$ [sudo] chgrp group file ...

Where:

  • sudo is optionally used to execute the chgrp command with superuser privileges to change the ownership of files that don't belong to the currently logged in user.
  • group is the name of the new group.
  • file ... is a list of paths to the files and directories you want to change the ownership of.

Notes:

  • When not using the sudo command, the user must be part of the new group in order to be allowed to change it.

  • As a reminder, you can verify which groups a user belongs to using the groups command.

Example

Let's consider the following script.sh file that belong to the user named john and the group named developers:

$ ls -l
-rw-r--r--  1 john  developers   0 Apr 22 16:18 script.sh

This command will transfer the ownership of the script.sh file to the group named testers:

$ chgrp testers script.sh

Summary

Here's a summary of what you've learned in this lesson:

  • File ownership refers to the user and group that have control over a particular file or directory through the use of file permissions.
  • The ls -l command is used to list files in the extended format.
  • The chown command is used to change the owner, and optionally the group, files and directories belongs to.
  • The chgrp command is used to change the group files and directories belonging to.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Managing File Ownership in Bash | Backend Brewery