Bash Cheat Sheet for Web Masters

Bash Scripting

Bash scripting is so useful; it allows us to take many mundane, common tasks like fixing permissions, setting up virtual hosts and deploying website versions and condense them to one line commands. Here is a bash cheat sheet of some of the more simple one-liners that I’ve run across in my time. Feel free to comment on them, or combine them in your own ways to create awesome scripts!

The Bash Cheat Sheet

~# chown www-data:kkeiper . -Rfh
Change ownership of all files within the current directory to the webserver and my group, recursively and following symlinks

~# find . -type f -exec chmod 0664 {} \;
Find all files within the current directory and change the permissions to User Readable + Writable, Group Readable + Writable, Other Readable

~# find . -type d -exec chmod 0775 {} \;
Find all directories within the current directory and make them traversable to all, writable to User and Group

~# grep -inr "eval(" . --color --include=*.php
grep through the current directory recursively, finding all usage of “eval(” within php files. Useful for finding potential hacks, since using eval() legitimately is frowned upon. Replace “eval(” with other functions that might be trouble, such as “base64_decode(“, etc.

~# tar -zcvf archive-name.tgz docroot
Make a file backup of a website’s Document Root. Couple this with the next one-liner to make a backup script that runs in 3 seconds!

~# mysqldump -uroot -p -h localhost databasename > databasename.sql
Create a SQL statement filled file to recreate the requested database, in its current form. Includes data + structure.

~# mailq
Show all outbound mail in the sending queue

~# postcat -q [MessageID]
Show headers and message sent in [MessageID]. MessageID can be gotten from mailq

~# postsuper -d [MessageID]
Delete the given message.

Leave a Reply

Your email address will not be published. Required fields are marked *

Thanks for your thoughts!