Recursive chmod Fun From The Shell
Here at Flatplanet Media, I run a network of a couple thousand affiliate websites. These sites are monetized two ways:
- Adsense Ads
- Amazon Affiliate Commissions
I use a program called phpZon to handle the amazon affiliate listings on each site. Last week I had to update phpZon for one reason or another…and the update went fine.
Or so I thought.
Apparently the new version cached amazon results a little differently than the old one. This is pretty important because the Amazon API only allows so many queries per hour. If you’ve got several thousand sites, even just a couple of visitors to each site can quickly max out your amazon api allowance.
When that happens, suddenly no amazon product listings get shown on any of your sites until the next hour, when the API limit resets itself.
…which is why the cache is so important. After you reach your api limit, the cache takes over and shows cached queries.
Except mine wasn’t doing it because somehow or another the permissions for the cache folder got screwed up in the update.
Basically every cache directory (a couple thousand of them) needed to have their permissions changed. The thought of doing that by hand was…uh…unappealing!
Solution?
Obviously I just needed to change the permissions on each cache folder, but when you’ve got a couple thousand websites, that means there’s a couple thousand cache folders. Doing it by hand would take forever.
It’s a problem that’s tailor made for a simple little shell script.
After a bit of Google searching, I came across a simple Bourne Shell Script that I slightly modified to suit my needs:
#!/bin/sh find /home -type d -name 'cache' -exec chmod 777 {} \;
The Code Breakdown:
#!/bin/sh
sets the path to the sh interpreter on your server.
find
We start out with the find command. You can run “man find” from your terminal to read the man page about find. It’s comprehensive and complete.
/home
tells the script which directory to start in.
-type d
tells the script what type of file to look for…in this case d stands for directory (you could use f if you needed to work with files instead).
-name 'cache'
tells the script the name of the file or directory to look for.
-exec chmod 777 {}\;
tells the script to execute the chmod command and set the directory permission to 777.
And that’s basically it!
I plunked that code into a file called mode.sh in my server’s /home directory, set it’s permission to 755 to make it executable, and then ran the command ./mode.sh
A few seconds later all couple thousand cache folders on my server had their permissions set to 777 and the cache now works fine for all the sites running phpZon.
Enjoy!
-John Elder