In a build script I want to create a temporary file that has a unique generated name, then write some data to it, and then rename that file to its final name (the reason for this procedure is that I want the renaming to happen (mostly) atomic).

But if I use mktemp, the new file will have very restrictive permissions:

Files are created u+rw, and directories u+rwx, minus umask restrictions.

... and these permissions will be preserved when the file is renamed. However, I want the resulting file to have "normal" permissions; that is, it should have the same permissions as files created by touch or by a shell redirection.

What is the recommended way to achieve this? Is there a (common) alternative to mktemp; or is there a simple and reliable way to change the file permissions to the normal umask permissions afterwards?

  • 2
    You can just run chmod on it, can't you? unix.stackexchange.com/a/249850 Commented May 25, 2021 at 9:49
  • 5
    Does this answer your question? reset permissions back to default Commented May 25, 2021 at 9:50
  • @ArtemS.Tashkinov: sorry for the late reaction. You are correct, resetting the permissions back to default is pretty much what I need! However the question you linked to doesn't mention the very elegant solution given by ilkkachu below, so I'm not sure if it's useful to mark this as a duplicate of that other question.
    – oliver
    Commented May 25, 2021 at 15:22

2 Answers 2


You can use chmod =rw "$file" after creating the temp file. The GNU man page says:

A combination of the letters ugoa controls which users' access to the file will be changed: [...] If none of these are given, the effect is as if (a) were given, but bits that are set in the umask are not affected.

So, =rw gives read-write permissions reduced by the umask, similar to passing 0666 as the permissions argument to open(), which is what e.g. touch filename, or echo > filename would do.

That's a POSIX feature, so other implementations should support it too.


Use this link to get the desired permissions you want http://permissions-calculator.org/


Create a script file that creates a new file, then changes its permissions.



#Create a temp file
touch $Filename

#Change the file's permissions (Use your own desired permissions)
chmod $Perms $Filename

After making the script executable chmod +x myscript.sh, execute the script file like this ./myscript.sh tempfile 600

Using Umask (Don't Recommend)

you can temporarily change the default file permissions using umask 600 for example.

To permanently change the default file permissions, edit your .bashrc file in your user's root directory.

nano ~/.bashrc

add umask 600 to the bottom of the file. Ofcourse, make sure to match your desired permissions. Make sure you edit the correct user as well.

If this is for a build script, I would use the manual method instead of relying on umask. Especially if your build script will be used on different systems.

  • 1
    umask probably doesn't help with mktemp. At least the GNU version says "Files are created u+rw, and directories u+rwx, minus umask restrictions.", so you can't use the umask to get permissions for group and others
    – ilkkachu
    Commented May 25, 2021 at 14:54
  • You are right, i included it just because they asked about it in the question. I updated the answer to reflect that ^^
    – Toasty140
    Commented May 25, 2021 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .