diff --git a/blog - fun with mysql.md b/blog - fun with mysql.md new file mode 100644 index 0000000..9d884bd --- /dev/null +++ b/blog - fun with mysql.md @@ -0,0 +1,24 @@ +# MySQL Tricks + +## dump all MySQL databases at once + +```bash +for i in $(mysqlshow -uroot -p | awk '{print $2}' | grep -v Databases); do mysqldump -uroot -p $i > $i.sql; done +``` + +## mysqldump without the artwork + +Often you want to get results from the mysql client without all the ASCII box table formatting and column names. + +The `-B` or `--batch` option will force the output to be TAB delimited no matter where the output is going. + +The `-N` option will turn off column names in the output. + +The following outputs the result of the query in TAB delimited format without column names: + +```bash +mysql -B -N -e "select distinct table_schema from information_schema.tables" +``` + +Tags: + howto \ No newline at end of file diff --git a/blog - fun with the Linux command line.md b/blog - fun with the Linux command line.md new file mode 100644 index 0000000..ebef336 --- /dev/null +++ b/blog - fun with the Linux command line.md @@ -0,0 +1,93 @@ +# Linux tips, one-liners and magic-tricks + +## Truncate a text file + +This is good for emptying huge log files without actually deleting and re-creating the file + +```bash +echo -n '' > {target file} +``` + +For example + +```bash +echo -n '' > /var/www/vhosts/horriblewebsite.com/statistics/logs/error_log +``` + +--- + +## Grep One-Liners + +How to find specific things with grep + +### find what looks like an IP Address + +```bash +egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" +``` + +### find what looks like an email address + +```bash +egrep "\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}" +``` + +### find what looks like a URL + +```bash +egrep -o "(http|https)://[a-zA-Z0-9./?=_-]*" +``` + +--- + +## mempigs + +A one-liner that will return the top 10 RAM consuming processes on a Linux machine + +```bash +ps aux  | awk '{print $6/1024 " MB\t\t" $11 " " $12}'  | sort -n | tail -10 +``` + +Alias it with something like this: + +```bash +alias mempigs="ps aux | awk '{print \$6/1024 \" MB\t\t\" \$11 \" \" \$12}'  | sort -n | tail -10" +``` + +Then you only have to type **mempigs** to get a quick overview of what's eating all the memory. + +--- + +## Filter comments and blank lines with grep + +There are times when you may want to check the contents of a configuration file, but it's littered with explanatory comments. Normally, documentation in a file is a good thing, but sometimes you just want to see the relevant settings. + +Filter out comments with something like this: + +```bash +grep -v \# /etc/some-config-file.conf +``` + +The grep flag -v excludes the next argument from the returned results. Because the pound sign is a common bash idiom that denotes a comment, you need to escape it in the above example, or everything in the command after the pound sign would be considered a comment! + +So, you've filtered out all the comments. But now you're left with lines separated by blank lines where the comments used to be. + +How to remove those blank lines? Add another grep to the command: + +```bash +grep -v \# /etc/some-config-file.conf | grep -v -e '^$' +``` + +The last grep uses the -v again to remove the next argument, but in this case, we are removing the regex (denoted by -e) `'^$'` which looks for the beginning of a line followed by the end of the line (in other words a line with no content, a blank line) + +--- + +## Shrink and convert PDFs with ghostscript + +REQ: ghostscript + +```bash +gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf +``` + +If the `-dPDFSETTINGS=/screen` setting is too low quality to suit your needs, replace it with `-dPDFSETTINGS=/ebook` for better quality, but slightly larger PDFs. Delete the setting altogether for the high quality default. \ No newline at end of file