Single command to create a file and set its permission

后端 未结 8 1151
北恋
北恋 2021-01-31 08:23

I am using the following 2 commands to create a 0B file and set its extn to 644

touch filename.ext
chmod 777 filename.txt

My question is that w

8条回答
  •  被撕碎了的回忆
    2021-01-31 08:38

    The only reason your files are not created with 666 permissions right off of the bat is the umask. So if you disable the umask:

    umask 0
    

    then when you touch the file it will end up with permissions 666 automatically. It would not be a good idea to make text files executable usually. Directories would end up with 777 permission with umask disabled.

    chicks@freecandy /tmp $ umask
    0022
    chicks@freecandy /tmp $ touch x
    chicks@freecandy /tmp $ mkdir xx
    
    chicks@freecandy /tmp $ umask 0
    chicks@freecandy /tmp $ touch y
    chicks@freecandy /tmp $ mkdir yy
    
    chicks@freecandy /tmp $ ls -ld x xx y yy                                                                                  
    -rw-r--r-- 1 chicks chicks  484 Jan 24 14:37 x
    drwxr-xr-x 2 chicks chicks 4096 Jan 24 14:37 xx
    -rw-rw-rw- 1 chicks chicks    0 Jan 24 14:37 y
    drwxrwxrwx 2 chicks chicks 4096 Jan 24 14:37 yy
    

提交回复
热议问题