When creating directories, mkdir -m <mode> <dir> provides for creating one or more directories with the given mode/permissions set (atomically).

Is there an equivalent for creating files, on the command line?

Something akin to:

open("file", O_WRONLY | O_APPEND | O_CREAT, 0777);

Is using touch followed by a chmod my only option here?

Edit: After trying out teppic's suggestion to use install, I ran it through strace to see how close to atomic it was. The answer is, not very:

$ strace install -m 777 /dev/null newfile
open("newfile", O_WRONLY|O_CREAT|O_EXCL, 0666) = 4
fstat(4, {st_mode=S_IFREG|0666, st_size=0, ...}) = 0
fchmod(4, 0600)                         = 0
close(4)                                = 0
chmod("newfile", 0777)                  = 0

Still, it's a single shell command and one I didn't know before.


You could use the install command (part of GNU coreutils) with a dummy file, e.g.

$ install -b -m 755 /dev/null newfile

The -b option backs up 'newfile' if it already exists. You can use this command to set the owner as well.

  • 4
    +1 for introducing me to install – quornian Sep 7 '12 at 15:37
  • It's a useful little command. As its name suggests it's really designed for installing software compiled from source code, to save having to use chmod, chown, and so forth all the time. – teppic Sep 7 '12 at 18:29
  • If only it took stdin! OK, /proc/self/fd/0 will do for my purpose. – proski Oct 28 '16 at 18:52

touch always creates the file if it doesn't exist, always follows symbolic links, and always makes the file non-executable. You can decide the read and write bits through the umask.

(umask 077; touch file)  # creates a 600 (rw-------) file
(umask 002; touch file)  # creates a 664 (rw-rw-r--) file

“Safe” atomic file creation (in particular, with O_NOFOLLOW) is not possible with traditional shell tools. You can use sysopen in perl. If you have the BSD-inspired mktemp utility, it creates a file atomically with O_NOFOLLOW; you'll have to to call chmod afterwards if the default mode of 600 is not the right one.

  • I was in the process of writing out a more detailed explanation, but the wikipedia articles covers everything just fine. – jordanm Sep 5 '12 at 1:16
  • If only touch had the option of creating executable files, that's really what I'm after. Then umask could be used to tailor the details. Sadly there is no way with umask and touch to create executables. – quornian Sep 7 '12 at 15:42
  • @quornian You can't create a non-empty file atomically anyway. What's the point of creating an empty executable file atomically? Use touch followed by chmod +x. – Gilles Sep 7 '12 at 15:44
  • 4
    You can create non-empty, executables files atomically with... ln or mv. You can always create the file with the right content and permissions in a directory created with umask 077 and move it or ln it afterwards. – Stéphane Chazelas Sep 9 '12 at 22:45

I have a bash script in my home directory /home/anthony/touchmod.sh containing:


touch "$2"
chmod "$1" "$2"

So if I need to create readme.txt with 644 permissions I can type:

~/touchmod.sh 644 readme.txt
  • 3
    You likely did not understand the question. This is exactly what the questioner tries to avoid. – ceving Aug 24 '16 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.