Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Different Umask For Files And Folders

I am writing a bash script to update some files/directories and I want to run umask in the script to set default permissions for files/directories created by the script. I know I can umask to set the permissions for both files and directories however I need the permissions to be different for files and folders.

I want files to be:   -rw----r-- (0604)
I want folders to be: drwx-----x (0701)

Can I do this using umask? If so how do I go about doing it? If not what is the best way to achieve it? Thank you in advance.

like image 675
cryptic ツ Avatar asked Sep 16 '12 08:09

cryptic ツ


People also ask

Does umask apply on directories?

The default umask for the root user is 022 result into default directory permissions are 755 and default file permissions are 644. For directories, the base permissions are (rwxrwxrwx) 0777 and for files they are 0666 (rw-rw-rw).

What is use of umask & What is default umask of files & directories?

umask (user file-creation mode) is a Linux command that lets you set up default permissions for newly created files and folders. 2. A user-defined permissions 'mask'. A user can choose how to restrict permissions by using a permissions mask.

What does umask 077 mean for a file?

umask 077. allow read, write, and execute permission for the file's owner, but prohibit read, write, and execute permission for everyone else.

What does umask 777 do?

The umask controls what permissions are not given to newly created file system object. Without the umask , all new directories would be created with full 777 permissions, and all new files would be created with full 666 permissions.


2 Answers

Interesting requirement. Currently (at least in bash), umask is a global setting and you cannot set it based on object type.

One solution that comes to mind would be to set the umask to the file variant and then intercept calls to mkdir (such as with a user-created mkdir script earlier in the path) to do:

umask 0701 ; /path/to/real/mkdir $1 ; umask 0604

That way, assuming all your directory creations are done with mkdir, you can ensure they use a different umask setting.

Note that the script should probably be a little more robust such as restoring the previous umask rather than forcing it to 0604, and adding some better error checking and possibly the ability to handle multiple arguments.

But that's all detail, the framework above should be enough to get you started.

like image 192
paxdiablo Avatar answered Oct 07 '22 05:10

paxdiablo


The umask is an attribute of the process not of a file - that is part of UNIX architecture and is nothing todo with Bash, or any other shell program.

The real issue is that the programs you are using do not allow the permissions to be changed on creation. In C, for example, mkdir has a second parameter, the mode.

You don't need to write C though, Python and Perl allow you to use the low-level interfaces. The permissions will be modified by the process's umask so, if you don't want any modification, set unmask to zero.

/home/user1> umask 000
/home/user1> python -c 'import os;os.mkdir("mydir",0701)'
/home/user1> ls -ld mydir
drwx-----x 2 user1 QAPLADV 4096 Sep 16 10:28 mydir

/home/user1> python -c 'import os;os.open("myfile",os.O_CREAT,0604)'
/home/user1> ls -l myfile
-rw----r-- 1 user1 QAPLADV 0 Sep 16 10:32 myfile

Don't forget that umask is still 000 at this point, you might want to set it back to its previous value if you are doing any other work in the same process.

Here is a Perl version if you prefer:

perl -e "mkdir mydir,0701"
perl -MFcntl -e 'sysopen(my $h,"myfile",O_EXCL|O_CREAT,0604)'

Of course if you have a large number of files, and you are likely to be running this often, then you would be much better off writing a Perl or Python program to do the job - calling perl or python for each file is a tad inefficient.

like image 41
cdarke Avatar answered Oct 07 '22 06:10

cdarke