What is the difference between umask and chmod

unix

I am completely confused between umask and chmod. Both are used to give permissions to the files. But where exactly is the difference and when to use them.

I have read the online documentation but both are looking same to me.

umask: umask is used to set default file permissions. These permissions will be used to all subsequent files during their creation.
chmod : used to change file and directory permissions.

As per my understanding if for example file test.doc is created.

By default unix gives the file 022 umask code.

Now when I change it to chmod 666 test.doc I can change the permission level of this file.

Now what if i use umask 666 for the same file.

What difference it happens when I use chmod 666 and umask 666

Best Answer

The difference is that umask entails only new files. As you stated, umask sets the default permissions that a file/directory will have on creation time, but afterwards umask doesn't affect them anymore.

chmod, however, needs the file be created prior to be run.

Therefore, if you run umask, it will have no effect at all on existing files.

Related Question