To add read and write permissions for the owner and group on folder, open terminal and type:
chmod ug+rw "folder name"
Add the -R option to recursively change the permissions of the folder and all files and sub-folders:
chmod -R ug+rw "folder name"
Example:
sudo find "folder name" -exec chmod a+rwx {} ";" sudo find "folder name" -type d -exec chmod 755 {} ";" sudo find "folder name" -type f -exec chmod 644 {} ";"
When you’re inside the folder, remove "folder name"
and replace it with .
If you want to have all of your files be readable to the world (ie. it’s just a static set of HTML files/images), then use this command:
chmod -R a+r "folder name"
That will recursively go through all of the files & subdirectories and add read permission to them.
WARNING: Don’t do this for code that executes! Only files that should be visible by everybody.
Also, if you try to use
chmod -r some-options
(small -r
instead of -R
) you might end up removing read permissions for everyone.
File permission errors in Apache, editors etc. where only 777 works but not 775 for folders and 644 for files.
Errors occur in either Apache or your editor regarding permissions because Apache runs under a different user (www-data
) than you.
If you want to write to /var/www
, add yourself to the www-data
group and set umask+permissions accordingly.
- Add yourself to the
www-data
group:sudo adduser your-username www-data
- Change the ownership of the files in
/var/www
:sudo chown -R www-data:www-data /var/www
-
Change the umask, so newly created files by Apache grants write permissions to the group too. Add
umask 007
to/etc/apache2/envvars
. -
Grant yourself (technically, the group
www-data
) write permissions:sudo chmod -R g+w /var/www
.
To set the directories to 755 and either leave the files alone or set them to 644. you can use the find command. For example:
To change all the directories to 755 (-rwxr-xr-x
):
find /opt/lampp/htdocs -type d -exec chmod 755 {} \;
To change all the files to 644 (-rw-r--r--
):
find /opt/lampp/htdocs -type f -exec chmod 644 {} \;
chmod 644 {} \;
specifies the command that will be executed by find
for each file. {}
is replaced by the file path, and the semicolon denotes the end of the command (escaped, otherwise it would be interpreted by the shell instead of find
).
Notice the execute bit exclusively on the directories returned by find. The execute permission on a directory allows a user class to list the contents of that directory and to cd
into it. Generally speaking you want both r
and x
on a directory for it to be accessible to you, even though there might be strange edge cases where you’d set only one or the other.
If you want to set permissions on all files to a+r
, and all directories to a+x
, and do that recursively through the complete subdirectory tree, use:
chmod -R a+rX *
The X
(that is capital X
, not small x
!) is ignored for files (unless they are executable for someone already) but is used for directories. Uppercase X
means “make all directories executable” (but not files). Just note that *
will not match hidden files (names beginning with a dot). It may make more sense, then, to use .
(for the current directory).
For example, if you want to set all directories to 775 and all files to 664, you could use
chmod -R g+wX .