umask: create new files with specified file mode

March 8th, 2008 mysurface Posted in Admin, touch, umask | Hits: 46270 | 2 Comments »

Did you realized that every time we create a new file in Linux, it always been set with certain file mode?

Lets see this:

touch testing
ls -l testing
-rw-r--r-- 1 mysurface mysurface 0 2008-03-08 01:14 testing

Refers to the chmod examples post, read, write and executable permission value is 4, 2 and 1. In this case, the file mode value of testing is 644.

Executable mode will not enable by default, lets enable it.

chmod +x testing
ls -l testing
-rwxr-xr-x 1 mysurface mysurface 0 2008-03-08 01:16 testing

As you can see now, the file mode is 755.

To control the file mode for every newly created file, we can use umask. umask is the inverse of the file mode you want it to be, let say you want your file mode always 774 (-rwxrwxr–) , your umask will have to be 003. How to get it? 777 – 774 = 003.

To check the current umask we are having, just type:

umask
0022

The value padding 0 in front indicate that is octal value, therefore our current file mode is 755, because the executable is not enable by default, it then become 644 when the file was newly created.


Let change it to 0003, meaning we want it to be 774 (rwxrwxr–).

umask 0003
touch testing2
ls -l testing2
-rw-rw-r-- 1 mysurface mysurface 0 2008-03-08 01:27 testing2

Ok, the file mode is 664, lets enable executable permission.

chmod +x testing2
ls -l testing2
-rwxrwxr-- 1 hkong hkong 0 2008-03-08 01:27 testing2

Now we get it 774, although chmod +x , the other’s group executable mode is not enable, because your disable executable using umask. But of cause, if you force it with chmod o+x, it still allows you to set other’s group as executable.

So what do we use it for?
For example, let say we need to create a user where its created file must be able to allow its group member to read but not write and executable, and also do not allow other user to even read, we can change the umask of this user by inserting umask 0037 to it’s ~/.bashrc

2 Responses to “umask: create new files with specified file mode”

  1. [...] 8th 2008 7:48am [-] From: linux.byexamples.com [...]

  2. Very good article. I certainly love this site. Stick with it!

Leave a Reply