Chmod to allow read and write permissions for directory

I have created directories in root. I am looking for the chmod command to allow all users read and write permissions to a specific directory. I have done chmod 775 for a file but I need this for a directory. This includes permissions on all files and sub directories.


Solution 1:

0775 is rarely correct for a file. The following will add the appropriate desired permissions to the appropriate type, without disturbing other existing permissions:

find somedir \( -type d -exec chmod u+rwx,g+rwx,o+rx {} \; -o -type f -exec chmod u+rw,g+rw,o+r {} \; \)

See the man page for find to help decipher that.

Solution 2:

For all users to have read and write access, that would be 0777 which is a bit dangerous, especially if you are running a webserver. Like @unwind said:

chmod -R 0777 /mydirectory Will allow all users read and write access to all files and folders within that directory

Depending on your purpose, you may want to read about sticky bits, which allow all users to create new files, but not to delete or edit other files in a directory:

chmod +t /mydirectory

Also, in case you didn't know man chmod will bring up the manual page for the chmod command, which you can search for the text "recursive" by typing /recursive

Solution 3:

That's not how the Unix protection model works, you can't set permissions recursively. You need to set them on each directory, all the way "down".

Of course you can do the setting recursively, but that only means "go through and set these permissions on all files and folders below", which is not how I understand your question.

To do do that, use the -R option to chmod:

$ chmod -R 0755 /my-cool-directory