Can files be created with permissions set on the command line?
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.
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.