Ruby FileUtils mkdir_p mode - unexpected result

I am trying to use the :mode option in FileUtils.mkdir_p . However, I get unexpected results with 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 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 to create a directory with the required permissions without using shell commands?

+5
source share
2 answers

Ruby interprets permissions as an integer, not an octal number. The chmod command (and parameters passed to mkdir_p ) accept octal (or equivalent as a whole). If you add 0 to a number, Ruby will use it as an octal.

FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 02750

Or you can use an integer ( ruby -e 'puts 02750.to_i' displays 1512 ).

FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 1512

+5
source

this should work, it looks like the method accepts the resolution as an integer of base 10 and not octal (with 0 in front)

  require 'fileutils' FileUtils.mkdir_p '/this/is/my/full/path/tmp', :mode => 02750 

to check the resolution in humanoid format, this command works well for me stat -c "% a% n" *

+1
source

Source: https://habr.com/ru/post/1216015/


All Articles