Command Line: Add Write Permissions to All Files and Folders

chmod -R g+w *This is a tricky one.

Plenty of people are experts at command line wizardry in the Linux/UNIX/*nix world, and I'd say I'm pretty comfortable with it. Anything you want to do, there's usually some uber-command that does everything in one line, probably using the -exec option, it just requires some searching and a little trial and error (with caution).

But here's one I've had trouble with. We had a directory that had mixed permissions. Specifically, the user and group were set wrong, and the permissions were set to user-write only, or rw-r--r--. I wanted to change the user and group, and set the permissions to rw-rw-r--, so that the group can write as well.

So, the first problem wasn't that bad. I can use chown with the -R flag (recursive) to change to owner. So the first command looked like this:

chown -R user *

Then, the group can be similarly changed with the chgrp command:

chgrp -R group *

I might have needed to run one extra set of commands to catch any files that start with a dot (.), since they'll be ignored by the *.

Now, the tricky part. I can use chmod to change the file permissions. The thing is, files need rw-rw-r-- (664), but directories need rwxrwx-r-x (775). I can't just run chmod -R 664 * because that would mess up directories.

So, the first solution is a little clunkly...

You could use find with the -exec option. You'd have to run two commands, and here's what they look like:

find ./ -type f -exec chmod 664 {} \;
find ./ -type d -exec chmod 775 {} \;

So, to break this down, I'm using the find command on the current directory. -type f means I'm looking for files of type "file". So, it will only return files, not directories. Then I use the -exec option to run the chmod command. The {} are where the replacement goes in, and the \; is just standard syntax. The second command uses -type d, which searches only directories and excludes files.

It works, but there's actually a shorter, one-line command to do both operations. Turns out, chmod can take the octal values (000-777), or it can use "symbolic mode". Using this mod, 'a', 'u', 'g', 'o' stand for 'all', 'user', 'group', 'other', respectively, and permissions are added or taken away using plus (+) and minus (-).

This makes my command really simple. All I want to do is add write permissions for groups on all files and directories. Assuming files currently have a permission of rw-r--r-- (644), and directories have a permission of rwx-r-x-r-x (755), all I have to do is run this command:

chmod -R g+w *

g+w translates to "grant the group write permissions".

And that's it!

P.S. If this keeps happening (as it does to me), it may be a good idea to set the umask variable in your account profile. umask works a little weird, there's an article explaining it here, but the value I would need to add for 664 and 775 permissions by default is this:

umask 002



Isam's picture

Thanks a lot, this was very helpful

Peter's picture

Software Tailor have been very good to us. They're always there when we need them - to answer implementation questions and provide customer support.
Please find Software Development

john joseph palassery's picture

This is a very good article that you have posted here in this page. I really like the way the details are given n this page. The explanations to the algorithm are given in a very clear manner.
Microsoft problems

sepatu vans's picture

It's a great pleasure reading your post.It's full of information I am looking for and I love to post a comment that “The content of your post is awesome” Great work! sepatu vans original celana chino

irwan's picture

Programming! Irwan like this! ;)

bestproducts's picture

Nice article, simple website.. Great combination.. Like it very much,, how to cure a yeast infection | how to make your boobs bigger | how to grow taller

rai2014's picture

Interesting topic shown here, i am now working on it regularly here and would say keep the future posts like this continuously.
Guar korma and Microlam roasted korma

ram14's picture

Year 2013 has been quite eventful. SRSG also witnessed events and evolved in terms of business verticals, technologies, people and processes.
Apple product dealers in Guwahati
AKG Microphones dealers
Mobile broadcasting in india
Turnkey TV Channel Solutions

samsellloyd's picture

Thanks for the nice post. I am expecting some different idea from your side. You always represent some new thought in your post.
buy youtube subscribers from Thanks :)

Add new comment