This is basic for some of you, and greek for others.
Thanks to Chris Miler for the “find” portion.
There are a couple of things I regularly want to do when I upload site files to my staging site.
- Change the owner of the file to the FTP user (so I can easily FTP up any changes)
- Change the group owner to apache (so Apache can make changes—the actual user the Apache server uses varies, you have to check it)
- Change permissions of all files, so user and group can read and write them, and everybody can read them
- Change permissions of all directories, so user and group can read, write, and execute them, and everybody can read and exeute them.
There is no super-elegant, simple way to do this with one command. That is why the shell script.
If you are more paranoid or whatever, you want to lock down permissions for the group level too, but …
Now, I save the following file as permissions:
#!/bin/bash
user=$1
if [ $user ]
then
find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;
chown $user:apache .
chown -R $user:apache *
else
echo "You must provide a user as a parameter."
fi
To use it, you would type:
permissions <username>
.. where username is the username for the owner of the files, as you want it.
To make this really useful, you’ll want to put it in your path. In my account, I edit the ~/.bash_profile file, and add this line:
PATH=$PATH:$HOME/bin
.. and then, make yourself said directory,
$HOME/bin
.. it is a handy place to put scripts you want to run anywhere.
Then, whenever you upload / download new files, you can just type permissions <username>, and it will magically transform all of these files.