How to change file permissions for a directory in one command
unix
How can I change all the file permissions of a directory in one command in Unix?
Best Answer
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.
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.
So, you own your home direcory /home/myusername, and have rwx permissions on it.
But a cd /home/myusername
gives a Permission denied?
That looks like you are not allowed to look at /home in the to begin with, before it comes to actually looking up myusername in that directory.
With sudo chmod 774 .*
in /home/myusername, you included the file ...
And /home/myusername/.. is the same as /home.
The permissions were applied as root, so the 4 is what applies to you a a normal user. this is r--, it does not contain the execute permission. On a directory, the execute permission is what allows you to look inside the directory.
The problem applies to all users that have their home directory under /home, as you already found out.
Best Answer
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):To change the owner of a file/directory recursively (affecting all descendants):
To change permissions bits of all files in a directory, recursively:
To change permissions bits of all directories:
It would be nice if you could just do this:
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: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 usingsudo
, 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.