Change folders/files owner and permissions

In this post we will discuss how to change folders/files owner and permissions.

Change owner

To change the owner all of the sub-folder(s) and file(s) of a folder use the following commend:

cd your-prefered-folder
sudo chown -R username:username .

Here,
your-prefered-folder = directory of the folder which files and sub-folders you want to change owner
username = your user name

Change permission

To change permissions all of the sub-folder(s) and file(s) of a folder use the following commend:

cd your-prefered-folder

# chmod [OPTION] [MODE] [FILE]
sudo chmod -R 754 .
# Here, OPTION = -R, MODE = 754, FILE = . (all inside)

Here, “-R” means the permission will be applied recursively throughout the directory, which means the permission will be applied to all the files and directories inside it. The last dot means apply the permission to everything inside the directory.

The digits 7, 5, and 4 each individually represent the permissions for the user, group, and others, in that order. Each digit is a combination of the numbers 4, 2, 1, and 0:

  • 4 stands for “read”
  • 2 stands for “write”
  • 1 stands for “execute”
  • 0 stands for “no permission”

So, 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1 (read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute).

You can also use r, w, x for read, write and execute respectively. And u, g, o for user (owner), group and others respectively.

# Grant read, write & execute permission for the user (owner) and groups; and read permission for others
sudo chmod u=rwx,g=rwx,o=r demofile.txt

# Grant read, write & execute permission for the user (owner)
sudo chmod u+rwx demofile.txt

# Revoke execute permission for others
sudo chmod o-x demofile.txt

Source:

  1. https://linuxize.com/post/linux-chown-command/
  2. https://en.wikipedia.org/wiki/Chmod

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.