Chmod to allow read and write permissions for directory

62

16

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.

chrissygormley

Posted 2010-03-31T11:56:47.450

Reputation: 723

Answers

34

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.

Ignacio Vazquez-Abrams

Posted 2010-03-31T11:56:47.450

Reputation: 100 516

50

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

deadkarma

Posted 2010-03-31T11:56:47.450

Reputation:

15

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

unwind

Posted 2010-03-31T11:56:47.450

Reputation: 401

@unwind -- I mean every file and sub directory of a directory I chmod has read and write permission's for all. Is that what this command does? Thanks – None – 2010-03-31T12:02:24.277

1When I put the -r before the 0755 I get 0755 - no such file or directory.. putting the -r after solves my problem. – Tim Mottram – 2018-06-26T15:42:05.043

"do do". lol. nice. – ytpillai – 2018-07-03T15:32:41.150