Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
+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

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.

share|improve this answer
    
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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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