File Permissions via command line for Ubuntu

chmod command

Use this command to change file or directory attributes. If you do ls -l in the present working directory, you would see file attributes for your files and directories in the present working directory. To change the permissions you can use “chmod”.

chmod XXX <file-name>

or

chmod XXX <directory-name>

Each ‘X’ denotes a value for:

  • “user”
  • “group”
  • “others”

Values for X

‘X’ above can be assigned value from 0-7.

X=4: for read only.

X=2: for write only.

X=1: for execute only.

You can get a combination of permisssions eg. “read and write” by givng ‘X’ a combined value as ‘X=6’ (‘X=6’ is obtained by adding read and write value).

// read, write and execute permission
X:4+2+1=7

// read and write permission
X:4+2=6

// read only permission
X:4

Assigning permissions
Say you want to assign permission for a certain file or directory as:

  • User: Read and Write and Execute
  • Group: Read and Write
  • Others: Read Only

Then you can run the chmod as follows:

chmod 764 file.txt

chmod 764 test_dir

chgrp command

To change group attribute of a file or directory. Each user belongs to a group. If you need to change the group  the user belongs to use this command.

chgrp <group-name> <file-name>

or

chgrp <group-name> <directory-name>

For example, if you have file a file “file.txt” and directory “test_dir”, and you want to assign a group “admin” to it, you can do that by:

chgrp admin file.txt

chgrp admin test_dir

chown command

To change owner of a file or directory. It can also be used to change the file group along with the user.

chown <owner-name> <file-name>

or

chwon <owner-name> <directory-name>

Lets assume you have file a file “file.txt” and directory “test_dir”, and you want to assign a owner as “minto” to it, you can do that by:

chown jsbisht file.txt

chown jsbisht test_dir

Change owner with group

If you want to change user and group simultaneously:

chown <owner-name>:<group-name> <file-name>

or

chown <owner-name>:<group-name> <directory-name>

For example:

chown jsbisht:admin file.txt

chown jsbisht:admin test