Skip to content

Latest commit

 

History

History
220 lines (179 loc) · 5.24 KB

README.md

File metadata and controls

220 lines (179 loc) · 5.24 KB

Bits of Code

Collection of code snippets to make command line life a bit easier...

Table of Contents:

WordPress:

Enable updates when asked for FTP credentials

This happens when file/folder write permissions are set securely

Add this code to wp-config.php

From Stack Overflow

define('FS_METHOD', 'direct');

I love all you, precious plugins, slowing everything to a crawl, phoning home for fresh ads to show, so much.

From Twitter @Rarst

define( 'WP_HTTP_BLOCK_EXTERNAL', true );
define( 'WP_ACCESSIBLE_HOSTS', '*.wordpress.org' );

Stop WP embed script from loading

// Remove WP embed script
function ecotechie_stop_loading_wp_embed() {
    if ( ! is_admin() ) {
        wp_deregister_script( 'wp-embed' );
    }
}
add_action( 'init', 'ecotechie_stop_loading_wp_embed' );
/**
 * Conditionally change menus.
 */
function ecotechie_wp_nav_menu_args( $args = '' ) {
	// change the menu in the Header menu position.
	if ( 'top' === $args['theme_location'] && is_page( '138' ) ) {
		$args['menu'] = '4'; // 4 is the ID of the menu to use here
	}
	return $args;
}
add_filter( 'wp_nav_menu_args', 'ecotechie_wp_nav_menu_args' );

WP-CLI:

Convert all database tables to Innodb with WP-CLI

wp @ALIAS db query "SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.', TABLE_NAME, ' ENGINE=InnoDB;') FROM information_schema.TABLES WHERE ENGINE = 'MyISAM'" --skip-column-names | wp @ALIAS db query

PHP:

Quick PHP debug with wp_die()

Replace ... with variables to display on screen

wp_die( '<pre>' . print_r( array( ... ), 1 ) . '</pre>' );

.htaccess:

Attempt to load files from production if they're not in our local version

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule wp-content/uploads/(.*) \
    http://{PROD}/wp-content/uploads/$1 [NC,L]
</IfModule>

Bash Commands:

Generate public/private rsa key pair with (YOUR_EMAIL) as note, then output keys to screen

ssh-keygen -C YOUR_EMAIL -m PEM -f ./id_rsa && echo && cat id_rsa && echo && cat id_rsa.pub

Display new information as it is being written to a file (FILE_NAME)

tail --follow FILE_NAME

Display disk space information

df -hT

List directories sorted by total number of files recursively, starting with (.)

From StackExchange

find . -type d | while read dir; do echo "$dir" : $(find "$dir" -type f | wc -l); done | sort -k2 -t ':' -n;

List directories and files sorted by size

From StackOverflow

du -a -h --max-depth=1 | sort -h

Show total size of files of type (.jpg) recursively in a directory (.)

From StackExchange

find . -type f -name '*.jpg' -exec du -ch {} + | grep total$

Show total size of a directory (.)

From StackExchange

du -sh .

TTL value of a DNS record

From ShellHacks

Remaining

dig +noall +answer domain.com

Configured

DOMAIN=domain.com; dig +noall +answer $DOMAIN @$(dig NS $DOMAIN +short|head -n1)

Website response time

From ShellHacks

curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null domain.com

Print the public IP

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Compress PDF

ps2pdf in.pdf out.pdf

Search recursesively

Files modified on Jan 11 at 4am

find . -type f -ls | grep 'Jan 11 04'

Files edited in date range

find . -type f -newermt 2018-01-10 ! -newermt 2018-01-11

chmod recursevly

CAREFUL WITH THESE!!!

f = files, d = directories

find . -type f ! -perm 0644 -print0 | xargs -0 chmod 644
find . -type d ! -perm 755 -print0 | xargs -0 chmod 755

Find files and directories not owned by GROUPNAME, excluding some paths

find . ! -group GROUPNAME ! -path './APATH/' ! -path './ANOTHERPATH'

Bash Scripting:

Qick and dirty timer

From Unix Stack Exchange

start=`date +%s`
# Code to be timed...
end=`date +%s`

runtime=$((end-start))

or

start_time=$SECONDS
# Code to be timed...
elapsed_time=$(($SECONDS - $start_time))

“Yes/No” Menu

From Tec Admin

while true
do
	read -r -p "Is $this_variable correct? [Y/n] " input
	
	case $input in
		[yY][eE][sS]|[yY])
		break;;
		[nN][oO]|[nN])
		read -p "Enter variable again:" this_variable;;
		*)
		echo "Invalid input..."
		;;
	esac
done