Code Snippets

These are a collection of code snippets for common use cases. Processes that are often done repeatedly can be automated, and things that I repeat often or search for consistently are shared here.

Check for port usage Linix

Language: Shell
#!/bin/bash

# to check for port 8000
sudo lsof -i -P -n | grep 8000Copy to Clipbord
            

Reinstall PostgreSQL Ubuntu

Language: Shell
#/bin/bash

sudo apt-get update && sudo apt-get upgrade
sudo apt-get purge postgresql*
sudo apt-get -f install
sudo apt-get install postgresqlCopy to Clipbord
            

Download all images from a site

Language: Shell
#/bin/bash

wget -k https://www.example.com/
cat index.html | grep img | grep -Po 'src="\K.*?(?=")' | sed 's/\?.*//' > links.txt
wget -i links.txtCopy to Clipbord
            

Batch rename files with Bash

Language: Shell
#/bin/bash

for filename in *foo*; do mv "$filename" "${filename//foo/bar}"; doneCopy to Clipbord
            

Crop images as square in center with Mogrify

Language: Shell
#/bin/bash

mogrify -gravity Center -crop 680x680+0+0 *.jpgCopy to Clipbord
            

ImageMagik resize images by wildcard and preserve aspect ratio

Language: Shell
#/bin/bash

mogrify -geometry x600 *.pngCopy to Clipbord
            

Use webp in HTML with fallback

Language: HTML
<picture>
  <source srcset="img/example.webp" type="image/webp">
  <source srcset="img/example.jpg" type="image/jpeg"> 
  <img src="img/example.jpg" alt="Example Alt Text">
</picture>Copy to Clipbord
            

Export MySQL database on the command line

Language: Shell
#/bin/bash

mysqldump -u username -p database_name > database_name.sqlCopy to Clipbord
            

Export MySQL database on the command line

Language: Shell
#/bin/bash

mysqldump -u username -p database_name > database_name.sqlCopy to Clipbord
            

Show users and update password for PostgresSQL

Language: Shell
#/bin/bash

# conntect to postgres
psql postgres postgres

# list users 
# \du

# change password
# \password usernameCopy to Clipbord
            

Update the home and siteurl options in Wordpress on the command line

Language: SQL
SELECT * FROM wp_options WHERE option_name = 'home';
UPDATE wp_options SET option_value="http://new_host/wordpress" WHERE option_name = "home";

SELECT * FROM wp_options WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value="http://new_host/wordpress" WHERE option_name = "siteurl";Copy to Clipbord
            

Git merge unrelated histories

Language: Shell
#/bin/bash

git pull origin yourbranch --allow-unrelated-historiesCopy to Clipbord
            

Create new user on MariaDB

Language: Shell
#/bin/bash

sudo mysql -u root
# CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';
# GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';Copy to Clipbord
            

Install NodeJS on WSL

Language: Shell
#/bin/bash

# install cURL
sudo apt-get install curl

# install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

# install node
nvm install node

# install the latest version of node
nvm install --ltsCopy to Clipbord
            

Download Golang on Linux

Language: Shell
#/bin/bash

VERSION=1.14
OS=linux
ARCH=amd64

wget https://dl.google.com/go/go$VERSION.$OS-$ARCH.tar.gz
tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
export PATH=$PATH:/usr/local/go/binCopy to Clipbord
            

Setup SSH server on WSL and connect via PowerShell

Language: PowerShell Shell
ssh username@localhostCopy to Clipbord
            
#/bin/bash

# uninstall ssh server 
sudo apt remove openssh-server

# reinstall ssh server
sudo apt install openssh-server

# edit sshd_config file
sudo vim /etc/ssh/sshd_config
# change to the following: 
#
# PasswordAuthentication yes
# AllowUsers yourusername

# restart service 
sudo service ssh start
# or if running 
# sudo service ssh --full-restart

# allow to start without password
sudo visudo
# add %sudo ALL=NOPASSWD: /usr/sbin/sshd after %sudo  ALL=(ALL:ALL) ALL

# restart service
sudo service ssh --full-restart
Copy to Clipbord
            

Disable BitLocker on Windows 10

Language: PowerShell
$BLV = Get-BitLockerVolume
Disable-BitLocker -MountPoint $BLV

manage-bde -statusCopy to Clipbord
            

Recursively download website with wget

Language: Shell
#/bin/bash

wget \
     --recursive \
     --no-clobber \
     --page-requisites \
     --html-extension \
     --convert-links \
     --restrict-file-names=windows \
     --domains example.com \
     --no-parent \
         example.comCopy to Clipbord
            

Darken background image

Language: HTML
<div style="linear-gradient (rgba(0,0,0,0.2), rgba(0,0,0,0.2)), url('../yourimage.jpg')">
    <!-- Your content -->
</div>Copy to Clipbord
            

Enable mod_rewrite Apache

Language: Shell
# enable rewrite
sudo a2enmod rewrite

# edit apache2 conf 
sudo vim /etc/apache2/apache2.conf

# edit all directories to AllowOveride All
# example: 
# <Directory />
#        Options FollowSymLinks
#        AllowOverride All
#        Require all denied
# </Directory>Copy to Clipbord
            

Export and Import all MySQL databases

Language: SQL
mysqldump -u root -p --all-databases > alldb.sql

mysql -u root -p < alldb.sqlCopy to Clipbord
            

Recursively download directory using FTP and wget

Language: Shell
#/bin/bash

wget -m ftp://ftp.example.com/dir/* --ftp-user=the_username --ftp-password=the_password   Copy to Clipbord
            

Create a new user in MySQL

Language: SQL
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';Copy to Clipbord
            

Setup LAMP on WSL Ubuntu

Language: Shell
#/bin/bash

# add Apache
sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get update
sudo apt-get install apache2

# create symbolic link to directory on C:/ drive
sudo mkdir /mnt/c/public_html
sudo ln -s /mnt/c/public_html /var/www/devroot
echo 'Modify DocumentRoot to /var/www/devroot and ServerName to localhost'
sudo vim /etc/apache2/sites-enabled/000-default.conf

# install MariaDB server
sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://ams2.mirrors.digitalocean.com/mariadb/repo/10.2/ubuntu xenial main'
sudo apt-get install mariadb-server

# install PHP
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-mcrypt php7.1-mysql php7.1-mbstring php7.1-gettext php7.1-xml php7.1-json php7.1-curl php7.1-zip

# install phpmyadmin
sudo apt-get install phpmyadmin
sudo phpenmod mcryptsudo phpenmod mbstring

# start services
sudo service apache2 start
sudo service mysql start
mysql_secure_installation
sudo service apache2 restartCopy to Clipbord
            

Get rid of Windows 10 Bloatware

Language: PowerShell
# uninstall 3D Builder:
get-appxpackage *3dbuilder* | remove-appxpackage

# uninstall Alarms & Clock:
get-appxpackage *alarms* | remove-appxpackage

# uninstall App Connector:
get-appxpackage *appconnector* | remove-appxpackage

# uninstall App Installer:
get-appxpackage *appinstaller* | remove-appxpackage

# uninstall Calendar and Mail apps together:
get-appxpackage *communicationsapps* | remove-appxpackage

# uninstall Calculator:
get-appxpackage *calculator* | remove-appxpackage

# uninstall Camera:
get-appxpackage *camera* | remove-appxpackage

# uninstall Feedback Hub:
get-appxpackage *feedback* | remove-appxpackage

# uninstall Get Office:
get-appxpackage *officehub* | remove-appxpackage

# uninstall Get Started or Tips:
get-appxpackage *getstarted* | remove-appxpackage

# uninstall Get Skype:
get-appxpackage *skypeapp* | remove-appxpackage

# uninstall Groove Music:
get-appxpackage *zunemusic* | remove-appxpackage

# uninstall Groove Music and Movies & TV apps together:
get-appxpackage *zune* | remove-appxpackage

# uninstall Maps:
get-appxpackage *maps* | remove-appxpackage

# uninstall Messaging and Skype Video apps together:
get-appxpackage *messaging* | remove-appxpackage

# uninstall Microsoft Solitaire Collection:
get-appxpackage *solitaire* | remove-appxpackage

# uninstall Microsoft Wallet:
get-appxpackage *wallet* | remove-appxpackage

# uninstall Microsoft Wi-Fi:
get-appxpackage *connectivitystore* | remove-appxpackage

# uninstall Money:
get-appxpackage *bingfinance* | remove-appxpackage

# uninstall Money, News, Sports and Weather apps together:
get-appxpackage *bing* | remove-appxpackage

# uninstall Movies & TV:
get-appxpackage *zunevideo* | remove-appxpackage

# uninstall News:
get-appxpackage *bingnews* | remove-appxpackage

# uninstall OneNote:
get-appxpackage *onenote* | remove-appxpackage

# uninstall Paid Wi-Fi & Cellular:
get-appxpackage *oneconnect* | remove-appxpackage

# uninstall Paint 3D:
get-appxpackage *mspaint* | remove-appxpackage

# uninstall People:
get-appxpackage *people* | remove-appxpackage

# uninstall Phone:
get-appxpackage *commsphone* | remove-appxpackage

# uninstall Phone Companion:
get-appxpackage *windowsphone* | remove-appxpackage

# uninstall Phone and Phone Companion apps together:
get-appxpackage *phone* | remove-appxpackage

# uninstall Photos:
get-appxpackage *photos* | remove-appxpackage

# uninstall Sports:
get-appxpackage *bingsports* | remove-appxpackage

# uninstall Sticky Notes:
get-appxpackage *sticky* | remove-appxpackage

# uninstall Sway:
get-appxpackage *sway* | remove-appxpackage

# uninstall View 3D:
get-appxpackage *3d* | remove-appxpackage

# uninstall Voice Recorder:
get-appxpackage *soundrecorder* | remove-appxpackage

# uninstall Weather:
get-appxpackage *bingweather* | remove-appxpackage

# uninstall Windows Holographic:
get-appxpackage *holographic* | remove-appxpackage

# uninstall Windows Store: (Be very careful!)
# get-appxpackage *windowsstore* | remove-appxpackage

# uninstall Xbox:
get-appxpackage *xbox* | remove-appxpackageCopy to Clipbord
            

List and count all website directories bash

Language: Shell
# Print all sites
ls | grep "[a-z]*\.[a-z]*\/"

# Count sites
ls | grep "[a-z]*\.[a-z]*\/" | wc -lCopy to Clipbord
            

Cleanup and Reinstall MySQL on Ubuntu

Language: Shell
#!/bin/bash

sudo apt-get remove --purge 'mysql*'
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get install mysql-serverCopy to Clipbord
            

lists handles for all enqueued styles and scripts

Language: PHP
/*
 * Lists out the handles for all enqueued styles and scripts. 
 * Credit: http://wordpress.stackexchange.com/questions/54064/how-do-i-get-the-handle-for-all-enqueued-scripts
*/

function wp_inspect_scripts() {
	global $wp_scripts;
	echo '<pre><h1>Script Handles</h1><ul>';
	foreach( $wp_scripts->queue as $handle ) :
		echo '<li>' . $handle . '</li>';
	endforeach;
	echo '</ul></pre>';
}
add_action( 'wp_print_scripts', 'wp_inspect_scripts' );

function wp_inspect_styles() {
	global $wp_styles;
	echo '<pre><h1>Style Handles</h1><ul>';
	foreach( $wp_styles->queue as $handle ) :
		echo '<li>' . $handle . '</li>';
	endforeach;
	echo '</ul></pre>';
}
add_action( 'wp_print_styles', 'wp_inspect_styles' );Copy to Clipbord