I am trying to use the :mode
option in FileUtils.mkdir_p
. However, I am getting unexpected results using Ruby 2.1.0.
require 'fileutils'
FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 2750
Result:
d-wSrwxrwT 2 myuid users 4096 Mar 24 10:14 tmp
However, if I just call the shell commands with backticks I get the desired result:
`mkdir /this/is/my/full/path/tmp && chmod 2750 /this/is/my/full/path/tmp`
Result:
drwxr-s--- 2 myuid users 4096 Mar 24 10:16 tmp
How can I create the directory with the desired permissions without using shell commands?
Ruby is interpreting the permissions as an integer rather than an octal number. The chmod command (and the options passed to mkdir_p
) takes an octal (or the equivalent as an integer). If you prepend 0 to the number, Ruby will use it as an octal.
FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 02750
Or, you could use an integer (ruby -e 'puts 02750.to_i'
displays 1512
).
FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 1512
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With