# Chmod -R u+X in ruby

**URL:** <https://www.ruby-forum.com/t/chmod-r-u-x-in-ruby/144190>\
**Category:** Ruby\
**Created:** [July 26, 2008, 1:20pm UTC](https://www.ruby-forum.com/t/chmod-r-u-x-in-ruby/144190 "2008-07-26T13:20:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [July 26, 2008, 1:20pm UTC](https://www.ruby-forum.com/t/chmod-r-u-x-in-ruby/144190/1 "2008-07-26T13:20:51Z")

</div>

I want to do a recursive changemod. But the chmod in File and  
FileUtils allows only, at least as far as I have seen, to set  
absolute file permission. But I want not to make all Files  
executable and also do not want the same permissions for all files,  
I want to do a “relative” change of file permissions like with  
chmod -R u+X example\_dir  
Currently I use an  
system(“chmod -R u+X example\_dir”) and I would like to know if there is  
a way to do that “naturally” in ruby.

Regards,  
Tammo

---

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [July 26, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/chmod-r-u-x-in-ruby/144190/2 "2008-07-26T15:50:45Z")

</div>

Tammo T. wrote:

> Currently I use an  
> system(“chmod -R u+X example\_dir”) and I would like to know if there is  
> a way to do that “naturally” in ruby.

A combination of Pathname.glob(‘example\_dir/\*\*’) and  
Something.chmod(0766)

---

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [July 26, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/chmod-r-u-x-in-ruby/144190/3 "2008-07-26T16:20:38Z")

</div>

Phlip wrote:

> Tammo T. wrote:
> 
> > Currently I use an  
> > system(“chmod -R u+X example\_dir”) and I would like to know if there is  
> > a way to do that “naturally” in ruby.
> 
> A combination of Pathname.glob(‘example\_dir/\*\*’) and Something.chmod(0766)

But then propably all files below example\_dir would have the same file  
permission. What I would like to accomplish is to change only one bit of  
the permissions. E.g. with something like

-rw-r–r-- file1  
-rwxr–r-- file2  
-r-xr-x— file3  
and I want to add group write permission I would get  
-rw-rw-r-- file1  
-rwxrw-r-- file2  
-r-xrwx— file3

But when I use something like chmod(0766) all files have the same file  
permissions afterwarts. But maybe some of them shall stay executable and  
some not.

Regards,  
Tammo

---

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [July 28, 2008, 2:24am UTC](https://www.ruby-forum.com/t/chmod-r-u-x-in-ruby/144190/4 "2008-07-28T02:24:34Z")

</div>

Tammo T. wrote:

> Currently I use an  
> system(“chmod -R u+X example\_dir”) and I would like to know if there is  
> a way to do that “naturally” in ruby.

You should use File.stat(‘filename’).mode, a bitmask, and a bitwise  
‘and’.
