Command Line: Add Write Permissions to All Files and Folders
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

Comments
It is easily available at every toy shop but it could be expensive for some people. Behavior problems, nightmares, and difficulty sleeping may follow exposure to media violence. If you can't find one of these products, a toilet paper roll can be used for the same purpose.
Add new comment