# Newbie... help with permissions

**URL:** <https://www.ruby-forum.com/t/newbie-help-with-permissions/105979>\
**Category:** Ruby\
**Created:** [June 5, 2007, 7:25am UTC](https://www.ruby-forum.com/t/newbie-help-with-permissions/105979 "2007-06-05T07:25:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![micahphone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@micahphone](https://www.ruby-forum.com/u/micahphone)\
**Post date:** [June 5, 2007, 7:25am UTC](https://www.ruby-forum.com/t/newbie-help-with-permissions/105979/1 "2007-06-05T07:25:20Z")

</div>

Hey ya’ll. I’m brand new to Ruby. Just a few hours into “Programming  
Ruby” in fact. I’m loving ruby so far. I can’t wait to get into some  
GTK+ with ruby (since I do a lot of GTK+ stuff in C).

I work best by solving real-world problems I have. So… an easy one…  
deleting some log files on a client’s server that are over 30 days old.  
These log files are named by date and time. So here’s what I have:

#-------------------------------------------------------------------------------  
#! /usr/bin/env ruby

LOG\_DIR = ‘/var/custom\_logs/cache/’

def delete\_if\_old(file)  
t = (Time.new-(60_60_24\*30)).strftime(“%Y%m%d\_%H%M%S”).to\_i  
if file.to\_i \< t  
File.delete(LOG\_DIR + file)  
end  
end

Dir.foreach(LOG\_DIR) { |f| delete\_if\_old f }  
#-------------------------------------------------------------------------------

So, when I replace the delete with a puts, I can see that all the logic  
is fine. However, when I try to delete I get a permissions error. I’m  
running the script as a user that does have access to the directory and  
can delete those files. So, my assumption is that ruby is NOT running as  
this user (the files can ONLY be deleted by the user, not the group).  
With my php scripts on this server I use cgiwrap to allow rwx access to  
my scripts.

Is there anything I can do ruby-wise to allow this ruby script to delete  
this file or do I have to change how the files are logged to allow group  
access?

The error is something like:

… in `delete’: Operation not permitted - /var/custom\_logs/cache/.  
(Errno::EPERM)

Any help is appreciated. I guess I’m just trying to understand how the  
script is executed… as what user. May be more of a standard linux  
permissions questions but still seems odd to me that the user that’s  
executing the script can delete the files but eh script itself cannot.

–

- Micah C.

---

<div class="post-metadata">

**Author:** ![micahphone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@micahphone](https://www.ruby-forum.com/u/micahphone)\
**Post date:** [June 5, 2007, 7:46am UTC](https://www.ruby-forum.com/t/newbie-help-with-permissions/105979/2 "2007-06-05T07:46:21Z")

</div>

On Jun 4, 2007, at 10:24 PM, Micah C. wrote:

> Hey ya’ll.  
> Hey.

> I’m brand new to Ruby.  
> Welcome. This is my first post to this list. So we’re on a par.

> def delete\_if\_old(file)  
> t = (Time.new-(60_60_24\*30)).strftime("%Y%m%d\_%H%M%S").to\_i  
> You might want Time.now.to\_i + ( 60 \* 60 \* 24 \* 30 )

> if file.to\_i \< t  
> You’re calling to\_i on a String here. I bet that “if” clause always  
> hits.

> ```
> File.delete(LOG_DIR + file)
> 
> ```
> 
> end  
> end
> 
> ## Dir.foreach(LOG\_DIR) { |f| delete\_if\_old f } #---------------------------------------------------------------------

Is it possible that the first time delete\_if\_old is called, it’s  
passed “.” (dot)?

---

<div class="post-metadata">

**Author:** ![micahphone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@micahphone](https://www.ruby-forum.com/u/micahphone)\
**Post date:** [June 5, 2007, 8:38am UTC](https://www.ruby-forum.com/t/newbie-help-with-permissions/105979/3 "2007-06-05T08:38:27Z")

</div>

Okay-- that was my bad. I wasn’t filtering out ‘.’ and ‘…’

Here’s the code that works:

#-------------------------------------------------------------------------------

#! /usr/bin/env ruby

LOG\_DIR = ‘/var/custom\_logs/cache/’

def delete\_if\_old(file)  
t = (Time.new-(60_60_24\*30)).strftime(“%Y%m%d%H%M%S”).to\_i  
if file.to\_i \< t && file != ‘.’ && file != ‘…’  
puts 'Deleting: '+file  
File.delete(LOG\_DIR + file)  
end  
end

Dir.foreach(LOG\_DIR) { |f| delete\_if\_old f }  
#-------------------------------------------------------------------------------

Now, if anybody has any suggestions as to coding conventions or things I  
should do differntly I’m all ears. Just because this works, doesn’t mean  
I don’t want to know how to do it the way ruby programmers  
conventionally would approach this.

- Micah C.
