How to change file permissions for a directory in one command
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.
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.
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.