Skip to content

Instantly share code, notes, and snippets.

@archan937
Last active October 18, 2019 12:51
Show Gist options
  • Save archan937/d242e2be4d0e7aea53a6 to your computer and use it in GitHub Desktop.
Save archan937/d242e2be4d0e7aea53a6 to your computer and use it in GitHub Desktop.
A collection of handy one-liners and snippets

Handy-dandy

A collection of handy one-liners and snippets by Paul Engel

Execute Bash command within its own environment

bash -c 'source /path/to/your/.env; echo $MYENVVAR'

Execute Fish command within its own environment

fish -c 'source /path/to/your/.env; echo $MYENVVAR'

MySQL dump and Gzip

mysqldump -u root awesome_database | gzip -c | cat > ~/awesome_database.sql.gz

Gunzip and load MySQL dump

gunzip < ~/awesome_database.sql.gz | mysql -u root awesome_database

Create .tar.gz files of every directory

ls | xargs -Idir tar -czf dir.tar.gz dir

Unzip every .tar.gz file

ls *.tar.gz | xargs -Ifile tar -xzvf file

Store BigQuery query result in a BigQuery table

bq query --destination_table=<project_id>:<data_set>.<table> "SELECT foo, bar FROM [<project_id>:<data_set>.<table>]"

Dump BigQuery table as TSV on Google Storage

bq extract --project_id=<project_id> --destination_format=CSV -F"\t" <data_set>.<table> gs://my_bucket/file.tsv

Move files from one Google Storage bucket to another

gsutil -m mv gs://my_bucket/dir gs://other_bucket/dir

Select dates within period using MySQL

SELECT ADDDATE("1970-01-01", d0.i + (d1.i * 10) + (d2.i * 100) + (d3.i * 1000) + (d4.i * 10000)) day 
FROM
  (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) d0,
  (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) d1,
  (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) d2,
  (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) d3,
  (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) d4
HAVING day >= "2015-02-02" AND day <= "2015-04-26"

How long is a process running?

ps -p PID -o etime=

List installed MySQL packages on CentOS 6

yum list installed | grep -i mysql

Uninstall MySQL from CentOS 6

yum remove mysql mysql-*

Install MySQL Python on CentOS 6

sudo yum install MySQL-python

Truncate a file with sudo privilege

sudo truncate -s0 /path/to/file

List open connections on a Mac

lsof -i

List open connections on a Unix computer

netstat -a

Print nice and compact Git log

git log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Create an SSH tunnel to remote MySQL

ssh -L 3307:remote_mysql:3306 me@ssh_host

Clear buffer cache on Unix computer

free && sync && echo 3 > /proc/sys/vm/drop_caches && free

List which port corresponds to which process on a Mac

lsof -iTCP -sTCP:LISTEN -n -P
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment