Skip to main content

File permissions


Let us go back to ls command covered in a previous post. ls command displays the list of files/sub-directories in current directory. And we also know that ls -a lists all files including the hidden files.

Another commonly used option of ls is ls -l (long list) which gives us file name, its size, its date and time of creation, its owner, group and permissions. So what are these?

So we see that long listing ls gives us lot of information. 3rd column tells us the name of owner (called user) of the file. - You are the owner of all files created by you. 4th column tells us the group the user belongs to. (Your admin might have put you in students group ) And 5th column tells us size of files - in bytes. 6th column is date and time of last modification of file.

What are first and second columns in the list?

2nd column is number of links to the file. We talk about links later.

1st column is permissions of the file - who can read the file, who can write/modify file and who can execute file. And these are specified for user (i.e. owner) -  letters 4 to 6, group - next 3 letters and(all) others - last 3 letters. The presence of r indicates read permission, presence of w indicates write permission and x indicates execute permission.

So in the example shown, a.txt can be read by user and written by user (rw-), it can be read and written by group (rw-) and it can only be read by others (r--).

So do these things really work? Let us modify permission. Let us remove write permission to user.

We modified the permission of the file a.txt using chmod command. We removed write permission from user (chmod u-w). Now when we try to overwrite this file, we get an error. Even vi editor shows readonly file.

Change permissions

To change permissions of a file, we can use chmod command. 

There are two different ways of giving this command. 

1. You can use + or - to add or remove permissions - read(r), write(w) or execute(x) to user(u), group(g) or others (o)

     chmod u+x a.1          add execute permission to owner (user)
     chmod g-r  a.txt          remove read permission from group
     chmod o+w a.txt       add write permission to others (every one other than group members)
     chmod a+x  myfile.sh   add execute permission to all

The last command adds execute permission to the file myfile.sh to all (a) - that is user, group and others.

2. You can use an octal number to represent the permission

e.g.
        rwx rwx rwx
        110 110 100
Looking at this you will realise that number 664 means - read/write for user, read/write for group and only read for others.

So you use the command
        chmod  664 a.txt

By the way, the number 777 means read/write and execute permission for every one.

Comments