Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I change all the file permissions of a directory in one command in Unix?

share|improve this question

migrated from stackoverflow.com Nov 8 '10 at 17:56

This question came from our site for professional and enthusiast programmers.

    
Do you mean you want to change the permissions for all files in the directory? Do you want this to go into nested directories as well? Do you want to change the permissions of the directory(ies) itself, or just the files within? Lastly, did you try searching this site first before posting your question? –  Ether Nov 8 '10 at 17:44

3 Answers 3

To change permissions on a file or directory entry non-recursively, use the chmod command (see man chmod to read more about its specific options):

chmod +x dir  # Set a directory to be listable
chmod +x file # Set a file to be executable

To change the owner of a file/directory recursively (affecting all descendants):

chown -R username           dir # Recursively set user
chown -R username:groupname dir # Recursively set user and group

To change permissions bits of all files in a directory, recursively:

find dir -type f -exec chmod 644 {} ';' # make all files       rw-r-r-

To change permissions bits of all directories:

find dir -type d -exec chmod 755 {} ';' # make all directories rwxr-xr-x

It would be nice if you could just do this:

chmod -R 755 dir

However, this has problems. It treats files and directories the same. The above command makes directories listable and readable by all users, but it also makes all files executable, which is usually what you do not want to do.

If we change it to 644, we get another problem:

$ chmod -R 644 x2
chmod: cannot access `x2/authors.html': Permission denied
chmod: cannot access `x2/day_of_week.plot': Permission denied
chmod: cannot access `x2/day_of_week.dat': Permission denied
chmod: cannot access `x2/commits_by_year.png': Permission denied
chmod: cannot access `x2/index.html': Permission denied
chmod: cannot access `x2/commits_by_year.plot': Permission denied
chmod: cannot access `x2/commits_by_year_month.plot': Permission denied
chmod: cannot access `x2/files_by_date.png': Permission denied
chmod: cannot access `x2/files.html': Permission denied
...

The problem is that 644 takes out the directory list bit, and this side effect prevents further traversal of the file tree. You could work around this issue by using sudo, but you still end up with directories that are completely useless to non-root users.

The point is, chmod -R works just fine in some cases (e.g. chmod -R g-r), but not in cases where you want to mess with the -x bit, since it operates on files and directories indiscriminately.

share|improve this answer
    
Why are you assuming that he wants to set 644 or that he is setting -x on the directories at all? The question only stipulates that he wants to change permissions, not to what in particular. –  Reese Moore Nov 8 '10 at 17:44
    
@Reese Moore: You're right. The asker didn't even say anything about changing permissions recursively. –  Joey Adams Nov 8 '10 at 17:51
    
You generally want to use the + and - symbolic mode stuff for that; ie, chmod -R go=u,go-w /dir –  dannysauer Nov 12 '10 at 0:37

chmod has a -R flag which means to change permissions on files and directories recursively.

You can use capital 'X' to do the right thing for folders: 'X' = "execute/search only if the file is a directory or already has execute permission for some user"

So, e.g.: chmod -R ug=rwX,o-rwx .

Would make an entire tree accessible to the owner and the group of each file, and not accessible to anyone else. Any already executable files would still be executable afterwards, and all directories would have the 'x' for the user and the group and not for others.

share|improve this answer
    
Try chmod -R 444 somedir on a directory with files in it. You get Permission denied because the -x flag is getting taken off of directories, and it's needed to continue reading the directory. –  Joey Adams Nov 8 '10 at 17:35
1  
A useful addendum to this is the X=Y syntax. You can for example set group permissions to the user permission using chmod -R g=u dir to set group and user equivalent. This works around things like dirs that need executable while "plain" files do not. –  Paul Rubel Nov 8 '10 at 17:52
chmod -R <file permission> *

From the man page of chmod:

-R, --recursive
              change files and directories recursively

Use chmod with -R switch for multiple directions which has sub directories tree having millions of files inside and you want to change the file permissions of these files together at one shot.

File permission may be for eg. 777, 755, 644 etc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.