0

I have a site, with some template directories, there are a few modules, each with their own template dir (So there are N dirs called templates).

Each of those dirs has a dir in it, called translated. I need a script to set all those dirs and it's contents writable.

I prefer this via a loop so I can echo the result. My shell skills are (still) very minimal, I can't seem to combine them, I can find the dirs from the current working directory, but I can't seem to get it recursive:

for f in ./*/translated/
do
    echo $f
done

This only finds ./templates/translated/, but not ./some/dirs/deeper/translated/

I can use $f now for a chmod a+rw, but this will only set the contents writable, how do I also get the dir itself? I need new files to be written in it too.

Summery:
1) How do I make it recursive?
2) How do I set the dir itself to +rw?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Martijn
  • 15,791
  • 4
  • 36
  • 68
  • 1
    I rolled back your edit. Your additional question is significant enough to be posted as a separate question. When you post a new question, feel free to add a link back to this question if you feel that it is useful as background. – tripleee Jan 29 '15 at 09:55

1 Answers1

2

The tool for finding stuff recursively is called find.

find . -name 'translated' -type d -exec echo chmod -R u+rw {} +

Take out the echo if you are satisfied with the results. If your find does not support -exec ... + then try with -exec ... \; instead.

Some shells have a wildcard ** which will do the same thing, but then your script will be tied to that particular shell.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Good training-wheels idiom: "echo first and remove the echo if happy with it" – cnicutar Jan 29 '15 at 09:51
  • Not really the "echo each found dir" method I hoped, but this is nice and simple, less code means less room for error :) – Martijn Jan 29 '15 at 10:09
  • You can put `chmod -v` to see what it's doing, or loop on the output from `find` if you replace the `-exec` with `-print` (or `-print0` if you have GNU `find`; see the manual for the benefits and implications). Quick googling should find you tens of thousands of examples now that you know the basics. – tripleee Jan 29 '15 at 10:12