Useful bash scripts
Just some personally useful bash scripts:
Find and delete files using a pattern
Find all files containing a particular string and do something with them - in this case, delete all files like "._*" i.e. "._user.module". This is usually junk left over from dead SSH sessions
for FILE in $(find . | grep '\._'); do rm $FILE; done;
Extract and import dumped SQL
This one comes from my work on gamerswithjobs.com, where I frequently received database dumps in the form of table dumps and need to decompress the import the SQL into a local DB. Also useful for bulk archiving or decompressing a large number of files.
echo "Extracting archives..."; for FILE in `ls *.gz`; do echo "Extracting $FILE ..."; gzip -d $FILE; done; echo "Importing SQL..."; for FILE in `ls *.sql`; do echo "Importing $FILE ..."; mysql -h dbhost -u dbuser -pdbpass dbname < $FILE; done;
Dump and compress database tables
Another useful one from gamerswithjobs.com work. This one dump each table from a database as an individual .sql file. This is particularly useful for extremely large databases where a straight dump of the entire DB would take longer than acceptable, either by potential crashes or by sys admin enforcement of a max execution time of commands.
echo "Dumping tables..."; for TBL in `mysql -e "show tables" -N -h dbhost -u dbuser -pdbpass dbname`; do echo "Dumping $TBL"; mysqldump -h dbhost -u dbuser -pdbpass dbname $TBL > $TBL.sql; done; echo "Compressing dumped tables..."; for FILE in `ls *.sql`; do echo "Compressing $FILE ..."; gzip $FILE; done;

Comments
No need to use grep or a for loop for that first task on most systems, find can do it all for you...
find . -name '._*' -exec rm {} \;or if you want confirmation on before delete (better safe than sorry)...
find . -name '._*' -ok rm {} \;The others could be condensed and made a little more fool-proof as well, but I'm sure work well enough when used in very specific circumstances. But then, whatever works, right? The shell gives you lots of ways to do everything.
Ah, slick - thanks for that tip. While I've been fumbling around the Unix command line for a few years now, there are still some basic things I'm trying to catch up on.
I call this my 'basher'
while true;
do
echo ""
echo "Hello! 80)"
echo ""
echo "1 - Start Apache & MySql"
echo "2 - Restart Apache & MySql"
echo "3 - Stop Apache & MySql"
echo "5 - Edit 000-default"
echo "6 - Edit hosts"
echo "q - Quit"
echo ""
echo -n "Select option and press enter: "
read INP
echo ""
case $INP in
1) sudo /etc/init.d/apache2 start
sudo /etc/init.d/mysql start;;
2) sudo /etc/init.d/apache2 restart
sudo /etc/init.d/mysql restart;;
3) sudo /etc/init.d/apache2 stop
sudo /etc/init.d/mysql stop;;
5) sudo geany /etc/apache2/sites-enabled/000-default;;
6) geany /etc/hosts;;
q|Q) exit;;
esac
echo ""
echo ""
done
that is very juicy. Appreciate all the help. the find and delete with a pattern is very useful tip. thanks
Find the best deals on windows, windows 7 key enterprise.Also find compatible computers,
windows 7 product key,devices,
office 2010 key and peripherals that have been pre-screened for performance. office 2007 key.
Post new comment