Ask Your Question
0

How to recursively chmod files in directory ?

asked 2016-07-04 10:35:23 -0500

cdm gravatar image

I'm new to puppet so bear with me. Ultimately, I'd like to chmod all files in a particular directory with one kind of permission and chmod all sub-directories within that same directory with another kind of permission.

Thanks. Chris

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2016-07-05 08:22:16 -0500

scoffland gravatar image

updated 2016-07-05 17:24:28 -0500

Puppet can manage files recursively. But it can not declare a resource more than once. I would manage the files in the parent directory with file resources individually assuming there are not too many of them and then manage the child directories with recurse = true.

Update: Puppet can now use the recurselimit option to specify the depth. With this code you can manage all files including the folders in the parent dir with recurselimit => 1. Child dirs can have their own resource with recurse = true. The following code shoudl be what you need.

  #Files in parent dir with mode 644   file { "/mnt/foo":
    owner => "root",
    ensure => directory,
    recurse => true,
    recurselimit => 1
    group => "root",
    mode => 0644,   }

  # Child dirs with mode 755 
  file { "/mnt/foo/bar":
    ensure => directory,
    recurse => true,
    owner => "root",
    group => "root",
    mode => 0755,   }
  file { "/mnt/foo/baz":
    ensure => directory,
    recurse => true,
    owner => "root",
    group => "root",
    mode => 0755,   }

Comment on exec suggested usage for this. Puppet magic is in declaring a state and ensuring it stays that way. If you find yourself using execs often your puppet manifests to do something your not following the puppet style. Execs should rarely be used in puppet as they are mini scripts and not declarative. Your code should be be declarative not a series of scripts. There is almost always a better (more puppet sytle) way of doing things in a manifest than using an exec.

edit flag offensive delete link more

Comments

Scoffland, unfortunately there are many files and folders within the top folder to change. I tried doing it with exec { find ... -type f -exec chmod 644 {} \; } but it doesn't seem to work. I'm somewhat surprised this is not an obvious function that puppet can do. I need to read up. thanks.

cdm gravatar imagecdm ( 2016-07-05 09:19:50 -0500 )edit

What happens when you run the exec for find -exec?

Nizen gravatar imageNizen ( 2016-07-05 14:46:08 -0500 )edit

looks like it works now, my path was wrong.

cdm gravatar imagecdm ( 2016-07-05 22:15:28 -0500 )edit

Ahh, that makes sense. Glad things are working for you!

Nizen gravatar imageNizen ( 2016-07-06 08:41:26 -0500 )edit
1

Thanks, yeah, 'find' and 'chmod' are under /bin, not /usr/bin !

cdm gravatar imagecdm ( 2016-07-06 10:52:53 -0500 )edit
0

answered 2016-07-05 10:12:23 -0500

UBPClaw gravatar image

If you declare a file resource and set type to directory there is a recurse option. I havent used it so I cant say for sure that itll do what youre asking, but thats where Id start

File resource docs

edit flag offensive delete link more
-2

answered 2016-07-05 09:35:18 -0500

maynero gravatar image

How about using exec resource then chmod -R? Basically, the first chmod will recurse to the sub directories and files, then the second one will only change the parent directory.

exec { 'chmod -R 644 /your/directory':
  path => ['/usr/bin', '/usr/sbin',],
} ~>
exec { 'chmod 755 /your/directory':
  path => ['/usr/bin', '/usr/sbin',],
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-07-04 10:35:23 -0500

Seen: 3,142 times

Last updated: Jul 05 '16