How to set file permissions with touch command

It seems that whenever I create a file with touch the permissions are set to: -rw-r–r–.

Is there some way that I can configure the permissions with touch or does this have to be done after with a different command?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can modify your umask to allow (for most implementations) more read/write privileges, but not executable, since generally the requested permissions are 0666.

If your umask is 022, you’ll see touch make a 0644 file.

Interestingly, POSIX describes this behavior in terms of creat:

  1. If file does not exist:

    The creat() function is called with the following arguments:

    • The file operand is used as the path argument.
    • The value of the bitwise-inclusive OR of S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH is used as the mode argument.

and it is only by following the links to creat, then to open, noticing the mention of umask and back-tracking to open (and creat) to verify that umask is supposed to affect touch.

For umask to affect only the touch command, use a subshell:

(umask 066; touch private-file)
(umask 0; touch world-writable-file)
touch file-as-per-current-umask

(note that in any case, if the file existed beforehand, touch will not change its permissions, just update its timestamps).

Method 2

You can manipulate the umask. Generally it’s set to 022 which means when a user creates a file, it will get permission of 0644, you can manipulate umask according to your needs.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x