1

I use Go (golang) 1.14 on Ubuntu 18.04

the files arrange like this: parent/ , and it is the working directory.

when i use

os.MkdirAll("dir/subdir", 0777)

it does make the files looks like parent/dir/subdir

but the file permission of dir is drwxrwxr-x and not drwxrwxrwx as i expected. with 0666 permissions, i get permission denied.

Idan
  • 23
  • 6

1 Answers1

2

The default umask for Ubuntu is:

# umask

0002

so it will remove the w user-permissions from your mkdirs permissions.

Unset this, and your program should get the permissions you desire:

# umask 0

# go build -o mkd ./main.go && ./mkd

$ ls -al dir/

drwxrwxrwx 3 me me 4096 Mar 17 10:27 .
drwxrwxr-x 7 me me 4096 Mar 17 10:27 ..
drwxrwxrwx 2 me me 4096 Mar 17 10:27 subdir

Note: If you want to ensure you don't tamper with the umask and only do this for your exe, use the sub-shell technique:

# (umask 0 && ./mkd)

or launch via a wrapper script:

#!/bin/bash
cd `dirname "$0"`
umask 0
./mkd $*      # <- passes along any arguments
colm.anseo
  • 19,337
  • 4
  • 43
  • 52