🎓 A1Learner
🔍
0 of 50 lessons completed

Changing Owners

Changing owners gives a file a new owner. An owner is the user who controls a file.

A new file and its owner

A fresh file carries the name of its maker. stat prints the owner name first and the group name second.

$ cd ~/practice
$ touch own.txt
$ stat -c '%U %G %n' own.txt

Give the file a new owner

chown hands own.txt to alex. The group stays the same.

$ cd ~/practice
$ touch own.txt
$ chown alex own.txt
$ stat -c '%U %G %n' own.txt

Give the file a new group

chgrp hands the group to alex. The owner stays the same.

$ cd ~/practice
$ touch own2.txt
$ chgrp alex own2.txt
$ stat -c '%U %G %n' own2.txt

Example

$ cd ~/practice
$ touch own.txt
$ chown alex own.txt
$ chgrp alex own.txt
$ stat -c '%U %G %n' own.txt

Key points

  • Use chown to change the owner of a file.
  • Use chgrp to change the group of a file.
  • chown changes the owner and leaves the group alone.
  • chgrp changes the group and leaves the owner alone.
  • The file itself stays the same. Only the owner name changes.

In this example, touch makes own.txt. chown gives the file to alex. chgrp gives the group to alex. stat prints alex twice, once for the owner and once for the group. The names here come from this machine, so yours will differ.

Note

Without full rights, chown refuses the change.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command changes the owner of a file?