How to Change File Permissions of a File in Linux



Linux


In this article, we show how to change file permissions of a file in linux.

So if you're familiar with operating systems and file structure and so on, you would know that files come with a file permission attribute.

So we can set the file permission to whatever we want.

This is done in linux through the chmod function.

We specify chmod followed by the permission number and the file name.

So if we want to set a file, note1.txt, to a permission of 600, the line of code to do so would be as the following shown below.



This will change the file permissions of the file, note1.txt, to a file permission of 600.

A file permission is always specified as a number.

Common file permissions are 600 which represents it's a private file for the current user who is logged in in which the user can read and write to the file, 644 which means that everyone can read but only the current user can write, 700 which represents it's a private directory for the current logged in user, and 755 which means that everyone can read and write to the file and the current user logged in can read, write, and execute.

Read means being able to access a file so that you can read the contents of it.

Write means being able to access a file so that you can write new contents to it.

Execute means that a user can execute a script. This is not for static files such as HTML. This is for dynamic files such as PHP or Python scripts. To run these on a server, the file needs execute privileges. With execute privileges, we can run CGI scripts, such as PHP, Python, and Perl scripts.

So how do we determine what numerical code to use to set a file permission?

Well the numbers determined for a file permission are actually octal code. Each octal number has 3 binary digits.

This is shown below.

File permissions

So you can see that file permissions can have 4 numbers, even though most have 3.

If there are 4 numbers, the first number is the number specifying special attributes. We won't concentrate on that here.

Most file permissions have 3 numbers.

Each number is an octal number.

Each octal number is composed of 3 binary digits, which represent r w x, which is read, write, execute.

The first octal number represents the user privileges.

By setting all binary digits to 1s, 111, this represents that the user has read, write, and execute privileges.

The second octal number represents the group privileges.

By setting the first and last binary digits to 1s, 101, this represents that the group can read and execute, but not write.

The third octal number represents other (other users).

By setting the first and last binary digits to 1s, 101, this represents that other users can read and execute, but not write.

And this is how we can decide what file permissions we want to set a file to.

Below is actual code of us changing the file permissions of a file in linux.

Changing file permissions in linux

We use the ls -l command in order to view file permissions in linux.

You can see that the file permissions have changed for the file, plan.html

Before the file permissions were rw-rw-r--, which means the current user can read and write, the group can read and write, and the other users can only read.

We then changed the file permissions to 777, which grants read, write, and execute privileges to all users and the group.

Now when viewing file privileges, you see that the file, plan.html, is -rwxrwxrwx. This means that the all users and the group can read, write, and execute on this script.

And this is how we can change the file permissions in Linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...