-   Linux - Newbie (
-   -   Recursively chmod directories (

simonx 10-30-2007 11:18 AM

Recursively chmod directories do I?
I have a dir structure with .html, .php etc.
I can use:
chmod -R 644 *.html
chmod -R 755 *.php
ok, but can't find a way to change all dirs to e.g. 755
tried getting a list of dirs and pipe to chmod:
sudo ls -R | grep :$ | sed s/:// | chmod 755
(am running ubuntu)
just comes back chmod: missing operand after `755'

yours frustratedly

guelzimtr 10-30-2007 11:36 AM

you can just cd to the directory you want to change it's permissions and type:

chmod -R 755 ./

colucix 10-30-2007 11:49 AM

Piping a list of directories, that literally are strings separated by newlines, does not tell to chmod what to do. You can use xargs to build and execute commands from the standard input, as in

sudo ls -R | grep :$ | sed s/:// | xargs chmod 755
A better way to do can be

sudo find ./test_dir -type d -exec chmod 755 {} \;
this looks for directories under test_dir and execute the specified command on every item found. See man xargs and man find for details.

pixellany 10-30-2007 11:51 AM

You have directory names with extensions like .html?? seems unusual, but I'm not sure how it is relevant.

chmod -R will change everything (directories and files)--I think you are saying you want to change directories only.

I would suggest a loop starting with:
for f in `ls -R` do

what this does is make a list of values in the variable "f", you can then test each one using--eg--the "file" command. If the test shows it is a directory, then:
chmod 755 $f

Note: In your other code, you were grepping using ":$" I assume you meant this to mean ":" at the end of the line. Trouble is, the ":" is not part of the actual directory name---I suspect it is just the character that your terminal uses to designate a directory.

uncle-c 10-30-2007 12:23 PM


for i in
`ls -al | grep '^d' | awk ' NR > 2 ' | awk '{print $9}'` ;
do chmod 7XX $i ;

There must be a way of combining the two awk commands ?
i.e print only the 9th field of all but the first 2 lines ?
It would make the above script look a bit shorter.

matthewg42 10-30-2007 12:28 PM

I've always done it in two steps. The -print0 argument to find and the -0 argument to xargs mean that the list of files is de-limited with the NULL character - this can save you problems if there are file names with spaces in:

find /path/to/files -type d -print0 |xargs -0 chmod 755
find /path/to/files -type f -print0 |xargs -0 chmod 644

cconstantine 10-31-2007 01:01 PM

IGNORE ME: someone else posted this higher up...

why not just

find . -type d -exec chmod 0755 {} \;
pitfall: besure to back-escape that semicolon to protect it from your shell. The semi needs to go as an arg to find, not a command sep eaten by your shell :)

All times are GMT -5. The time now is 11:00 AM.