# Need help with a script to set permissions

## wilburpan

I have a directory that holds a lot of document/media files, called FileCabinet.  There are a lot of subdirectories and they are nested pretty deeply.

Here's my issue: I'd like to make sure that all of these files have 0644 permissions.  No problem, right?  I entered this command:

```
chmod 0644 -R FileCabinet
```

Which should convert everything in this folder to 0644, permission-wise.

Problem is, that command also converted the folder permissions to 644, which played havoc with the subdirectories.  I would get this issue:

```
ls -l FileCabinet

ls: cannot access FileCabinet/Pictures: Permission denied

ls: cannot access FileCabinet/Movies: Permission denied

ls: cannot access FileCabinet/Documents: Permission denied

total 0

?????????? ? ? ? ?                ? Documents

?????????? ? ? ? ?                ? Movies

?????????? ? ? ? ?                ? Pictures
```

Obviously, what happened is that with the last command, I changed the directories to 0644 as well, which my system doesn't like.  This is fixable -- I just change everything to 0755.

But is there a way to write a script that traverses the directory and changes everything to 0644 except for the directories themselves?

----------

## Nerevar

```
find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644
```

----------

## wilburpan

Thanks -- that did the trick!

I need to spend more time with the 'find' and 'xargs' commands.

----------

## alex.blackbit

 *Nerevar wrote:*   

> 
> 
> ```
> find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644
> ```
> ...

 

```
$ find <path> -type d -exec chmod 0755 {} \; && find <path> -type f -exec chmod 0644 {} \;
```

is possible too, IMHO a little bit more elegant.

----------

## Nerevar

 *alex.blackbit wrote:*   

>  *Nerevar wrote:*   
> 
> ```
> find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644
> ```
> ...

 

I don't like the exec switch, but if you insist on using it you should look at the "-exec command {} +" variant in the manpage. It will speed up the operation on directories with many files.

----------

## timeBandit

 *alex.blackbit wrote:*   

> 
> 
> ```
> $ find <path> -type d -exec chmod 0755 {} \; && find <path> -type f -exec chmod 0644 {} \;
> ```
> ...

 In addition, use + instead of \; to get behavior more like an xargs pipeline (fewer spawned processes).

Edit: Bah. Too slow. What he (Nerevar) said.   :Wink: 

----------

## alex.blackbit

k, thx.

----------

