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;