DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • 11 Agentic Testing Tools to Know in 2026
  • From Data Movement to Local Intelligence: The Shift from Centralized to Federated AI
  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS
  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing

How the Chown Command Works on Linux

Let's look at how the chown command works on Linux, and how to use it.

By 
Johnny Simpson user avatar
Johnny Simpson
·
Apr. 06, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
3.3K Views

Join the DZone community and get the full member experience.

Join For Free

In previous articles, we've covered how chmod works. In this article, we'll be covering how to use chown. chown is closely connected to chmod since it affects who owns a file, and which group it belongs to.

In our article on chmod we cover how different user types (i.e. owners, groups, and all other users) have different permission sets. That means who owns or in which group a file belongs, can affect who can run it, and might even affect if a system process can run or open a file.

The syntax for chown is shown below, where [OPTIONS] are optional settings, [USER[:GROUP]] are the user and group, and [FILE] is the file, files, directory or directories we wish to affect with the command.

Shell
 
chown [OPTIONS] [USER[:GROUP]] [FILE]

If we specify only one [USER[:GROUP]], then it is assumed that we are trying to change the owner only, whereas if we only type: GROUP with a colon followed by the group name, then it is assumed that only the group is changing.

How To Change the Owner of a File With Chown

At its most basic, we can change the owner of a particular file with chown. For example, the following will change the owner for file.txt to someOwner:

Shell
 
chown someOwner file.txt

If we want to change the group to someGroup, and owner to someOwner, then the following command will do that:

Shell
 
chown someOwner:someGroup file.txt

And if we want to change just the group to someGroup, then this will do it:

Shell
 
chown :someGroup file.txt

This code will also work for directories.

Change the Owner or Group for All Files in a Directory

If we wish to change the owner or group for files and directories within a directory, we have to use the -R option.

For example, the below code will change the owner and group for all files in the ./test directory to someOwner and someGroup respectively:

Shell
 
chown -R someOwner:someGroup ./test

Symbolic Links and Chown

There are three options that affect how symbolic links are affected by chown, and whether they are followed. These are -H, -P, and -L.

  • -L - follow every symbolic link.
  • -H - if the command line contains a reference to a specific symbolic link, then follow it.
  • -P - do not follow symbolic links (default). For example, the below will change all owners and groups within the ./test directory, and will traverse every symbolic link:
Shell
 
chown -RL someOwner:someGroup ./test

Using a Reference File To Change Owners and Groups

To use a reference file as a basis for the ownership and group of a file, use --reference. For example, below the file new-file.txt will use the owner and group from file.txt. This will not work by default on MacOS.

Shell
 
chown --reference=file.txt new-file.txt

Change Owner and Group if It Matches a Specific Owner and Group

If we want to change only owners or groups which match a specific pair, we can use --from. For example, the below will only change the user and group newOwner: newGroup if the file was originally set to someOwner: someGroup. This will not work by default on MacOS.

Shell
 
chown --from=someOwner:someGroup newOwner:newGroup new-file.txt

This follows the same rules as before, so if we only want to match on someGroup, we'd write --from=:someGroup. If we only wanted to match on someUser, we'd write --from=someUser.

Other Options for Chown

As well to the already mentioned options, there are a few others that may be useful:

  • -v - verbose - will show a message after each file change.
  • -f - suppresses most error messages.
  • -h - changes the owner and group of a symbolic link, rather than the file or directory it links to.
  • -c - similar to verbose, but only reports when a change is made.

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook