December 2015

Bash – how to run custom commands at script exit

Bash scripts can trap EXIT signal and run clean up commands before exit. This is useful if a user kills a script or the script read more

How to display a div in browser window center

To display a div in browser window center we can use css position value fixed. The left and top value of fixed position are relative read more

Install and setup Jenkins on Ubuntu Linux

Jenkins is an open source continuous integration (CI) tool written in java. It can be used to run (on-demand or periodic) build and test jobs read more

Json online validator

Online Json validator (json syntax/lint checker) Sample valid json strings Simple json string “hello” Simple json array [“hello1”, “hello2”] Simple json associative array { “key1″:”hello1”, read more

Gmail – how to send email from different address

Gmail can be used to send email as different address. It can be handy if you want to use your primary account to receive and read more

How to display auto fade out message using jQuery

Sometimes we need to display a message and then fade it out. jQuery fadeOut will make an element fadeOut (by animating css property opacity) will read more

Bash – how to get main program and current file dir location

Get main program directory location In case you have a program main.sh in /opt/tmp which includes utils.sh from /opt/tmp/dir1, then this code will return /opt/tmp read more

Javascript – catch errors using window.onerror

Javascript errors can be caught using window.onerror event handler. Whenever a javascript error occurs browser calls this function. In this tutorial we’ll write error handler read more

NodeJS – npm beginner tutorial

Npm (Node package manager) is used to install/uninstall and manage node packages. Here are some common commands when using npm on command line on Linux read more

How to install Imagemagick on Ubuntu Linux

Use apt-get to install imagemagick $ sudo apt-get install imagemagick List binaries in imagemagick $ dpkg -L imagemagick | grep bin /usr/bin /usr/bin/conjure.im6 /usr/bin/composite.im6 /usr/bin/import.im6 read more

Python selenium – execute javascript code

Selenium code in python can execute custom Javascript and also return values from webdriver (PhantomJS, Firefox, Chrome. etc.) context. Here is sample code which counts read more

Bash – how to use functions – quick tutorial

Using functions in bash is fairly useful to organise and reuse code. Here is quick beginner tutorial with examples on using bash functions. Declaring functions read more

How to install node.js on Ubuntu Linux

Update node source $ curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash – Check which nodejs will be installed $ apt-cache policy nodejs nodejs: Installed: (none) read more

How to install Imagemagick on Mac

Use brew to get information on latest imagemagick version $ brew info imagemagick imagemagick: stable 6.9.2-8 (bottled), HEAD Tools and libraries to manipulate images in read more

Mac brew – how to find latest available package without installing

Find available packages on Mac using brew $ brew search [package name] $ brew search lua lua lua51 luabind luajit homebrew/emacs/lua-mode homebrew/nginx/lua-nginx-module homebrew/versions/lua53 Get info read more

Mac – how to change default location for screenshots

Mac screenshots are saved on Desktop by default. It can cause Desktop to get cluttered very fast. Here are the steps to change default screenshot read more

Python pytest – using selenium with PhantomJS

Here is quick tutorial to use python selenium with PhantomJS as driver. We will fetch a page and assert that page title has certain keyword. read more

View your browser and device screen width/height – online

Browser window width and height Live data based on your current browser. Resizing browser window will change values here. Property Value outer width,height – x read more

Mac – how to take screenshots

Mac supports full screen, window and a specific selection for taking screenshots. You can either store screenshot in a file on Desktop or in clipboard. read more

PHP echo – comma (,) vs dot (.) performance benchmarks

PHP echo allow comma (,) for concatenating various values in addition to dot (.). This works only for echo though. It is sometimes discussed comma read more

Bash – how to check if a variable is set

To check if a variable is set or empty, we can use couple of approaches. One approach is to use empty string check (if [ read more

Bash – iterate over array

These are few ways of iterating on bash arrays to read and or modify array’s elements in bash on Linux or Mac. Here are some read more

Can global variables be modified in bash function?

Global variables can be accessed in bash function without declaring anything and when these are modified the behaviour depend on whether function is called in read more

Install Amazon EC2 api tools on Ubuntu Linux

Amazon EC2 api tools have fairly handy utilities to get information and perform various AWS tasks on your instance. Here are quick steps to install read more

Bash – set default value if a variable is empty

There are multiple ways to check if a variable is empty and then set it to some default value. Shell parameter expansion gives us an read more

How to setup password less git push for github

You can use user and password based authentication to push git changes to GitHub. But it is cumbersome to type user and password with every read more

How to create ssh public/private keys on command line

Command line utility ssh-keygen can be used to generate rsa or dsa public and private keys. By default it generates rsa keys. Here are commands read more

Generate ssh public key from private key on command line

In case you have lost your ssh public key, you can re-generate it from the private key you have. Here is the command to generate read more

CSS – how to align image and text in center vertically

We frequently need to center text and images when we are writing HTML/CSS. This can be done by using vertical-align:middle property on the child element read more

Mac – how to copy, alias, move using drag/drop and mouse pointer indicators for these

We frequently use drag and drop on Mac for copying, creating alias/shortcut or moving files from one folder to another. Mac shows very good mouse read more

PHP rawurlencode online

This form can be used to encode a url or string using php rawurlencode function. Additional notes PHP rawurlencode will encode space to %20 The read more

PHP urlencode online

This form can be used to encode a url or string using php urlencode function. Additional notes PHP urlencode will encode space to + The read more

Bash – variables in double quotes vs without quotes

Bash variables without double quotes can give surprising results. The special characters inside a variable when not double quoted, gets interpreted by bash. Few points read more

Bash – newline and other escape character in string

To have newline (\n) and other escape characters in bash strings, we can use $’string’ syntax (ANSI-C Quoting). $’string’ expands to string, with backslash-escaped characters read more

Bash – append text to a variable

Variable in string approach Use curly braces {} around variable (${VARNAME}) in case text begins with alphanumeric character. Concatenate variable with string Use =+ operator read more

Why and how to use Amazon route53 as DNS

Using Amazon route53 has a huge advantage that it offers a TTL of as low as 60 seconds. This means changing IP for your domain read more

Mac VirtualBox – enable copy paster from/to Windows 10

If you are running Windows 10 inside VirtualBox, you may want to enable copy/paster from windows and vice versa. This tutorial should work on any read more

Javascript – dump all handlers on window object

DOM window object has many javascript event handlers attached to it. It may be handy to dump and view all of these. Here is code read more

Curl – how to hide progress bar

Curl by default shows progress bar. To hide it -s option can be used. We can also add -S to show errors when -s option read more

Bash – how to stop at error

To stop a bash script at first error use set -e. To undo it use set +e.

Linux – sending mail from command using mailutils

Linux command line tools to send mails are very handy for sending mails from bash scripts. As an example you can send daily php error read more

How to view wordpress current version

If you want view the version of currently installed wordpress for a site, here are few approaches. View version using dashboard glance section If you read more

How to debug :hover in Chrome

At times we need to force hover state when we are debugging CSS in style inspector in Chrome developers tools. Chrome supports forcing a certain read more

PHP – convert dos newline to unix format

Files or strings in DOS format may have \r (carriage return) along with \n (newline). This may appear as ^M in some editors. Here is read more

HTML table with alternate color rows

We often need to display a table with alternate rows having different colors. We can do it either using CSS3 nth-child selector or using jQuery. read more

PHP – assign multi lines string to a variable

PHP supports Heredoc and Newdoc syntax. It is a convenient way to assign multiple lines string to a variable. This makes code more readable. Here read more

How to run windows 10 on Mac using VirtualBox

Windows 10 can be run in Mac using Virtualbox and Windows 10 OS trial image available from Microsoft’s site. Here are the steps to install read more

PHP – print array in one line

At times for large array values we want to log them in one line for better readability. Logging an array in one line is also read more

PHP – empty() vs isset() vs is_null() vs boolean check

PHP isset() and empty() are frequently used to check the values of variables. In some cases is_null is also used. Here is a table covering read more

Jquery – change table cells color based on value

It is sometimes useful to change color of table cells based on values for better presentation and readability. Here is quick code snippet to do read more

Mac – open Finder from terminal – quick tip

Often we want to open Finder window when we are working on Terminal. Here are few scenarios to open Finder window from command line terminal read more

Imagemagick – how to create animation on command line

Imagemagick is very powerful tool to process images on command line. If you have multiple images, you can use imagemagick convert utility on Mac or read more

PHP check if a filename is valid

Sometimes we need to check if a filename contains special character etc. One possible approach is to check for alphanumeric characters and few more characters read more

Mac – how to take timed screenshot

At times we need to take timed screenshots on Mac. Here are two approaches we can use to take timed screenshot on Mac. Screenshot using read more

Ubuntu – find file to package

Ubuntu dpkg-query can be used to find package name given a file name, its full path or partial name. Note that it does not follow read more

Mac Finder – how to search system hidden files

System files are hidden on Mac and they don’t show up in Finder search results by default. In case you end up searching stuff which read more

Bash – pass all arguments from one script to another

When writing a wrapper bash script, we often need to pass all arguments passed to the wrapper scrip to another script. Here is quick bash read more

WordPress – how to create custom tag cloud

WordPress has support for displaying tag cloud using wp_tag_cloud. It displays tag cloud and has support for including and excluding tags and specifying mininum and read more

Mac – search files on command line using mdfind

Mac spotlight and finder are great way to find files on Mac. But Mac also has command line utility mdfind (utility to query meta data) read more

How to enable telnet program on Windows 10

Windows does not have telnet program enabled by default. Telnet is a handy tool to check if a port is up or not at any read more

How to run a program as administrator on windows 10

Sometime we need to run a program as administrator to be able to perform root privileged tasks. e.g. Editing the windows hosts file is only read more

How to find python package’s file location

Here is simple python code snippet to see the file location of a python package on command line: $ python -c “import urllib2; print urllib2.__file__;” read more

How to auto forward gmail from one account to another

In case you have multiple gmail accounts you can forward email from one account to another. You can also choose to keep the forwarded email read more

PhantomJS – how to skip Google analytics code when fetching a web page

PhantomJS can be used to test websites and for automating various tasks. When testing a website in production, it may not be a good idea read more

WordPress mysql query to get all custom keys and values for a post

Handy mysql query to get all custom keys and values for a post in wordpress. SELECT wp_postmeta.* FROM wp_postmeta JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id read more

Curl – follow redirects

Curl is fairly handy command line utility to fetch a web page. It can also be used to automate various tasks in bash shell scripts. read more

How to crop an image using imagemagick convert

Imagemagick convert is pretty handy tool to manipulate images and can be used to crop an image. This can be useful in automation and also read more

How to check if a user has password on Linux

Any user created on Linux may or may not have a password. Linux stores passwords in encrypted format in /etc/shadow. Here is quick command line read more

Linux – providing sudo access to a users – some best practices

Giving sudo to a user is frequently occurring activity on Linux. Here are some scenarios and best practices for providing sudo to users on Linux. read more

WordPress how to check if a post is being viewed by admin

In WordPress you may want to do certain things only when admin user is viewing a post. Here is quick code snippet to know if read more

How to print javascript object to log

Often we need to print a javascript object to console log or somewhere else for debugging purpose. These are some handy ways to dump javascript read more

Phantomjs – how to check what resources are being requested by a page

Phantomjs lets you add hooks when it fetches a web page. Here is code snippet which can be used to print all resources phantomjs fetches read more

Bash check if file begins with a string

Bash code to check if file begins with a string Note the presence of double square brackets ([[ ]]) Bash code to check if file read more

Get file size in bytes on Linux

Sometime we need to get file size for the purpose of automation in bash shell script. Here are few ways to get a size of read more

Bash shell – check if file or directory exists

Bash code snippet to check if file exists FILE=/etc/passwd if [ -f $FILE ] ; then echo “$FILE exists and is regular file” else echo read more

How to compress screenshot images using imagemagick convert

Imagemagick convert can be used to compress and screenshot images by changing unique colors, image depth etc. Here are some ways to compress images created read more

Find image unique colors and bit depth using imagemagick identify

Print image unique colors and bit depth $ identify -format “UniqueColors: %k, BitDepth:%q\n” img1.png UniqueColors: 1694, BitDepth:16 Note that when can compress an image with read more

WordPress – how to make a post sticky on home page

In wordpress you can make any post sticky and make it appear on home page at top even when there is more recent posts. Here read more

How to show wordpress pages on front page with skip_home custom field

By default WordPress pages donot show up on home page. Only posts are displayed on home page. Here is wordpress php code which can be read more

Fetch wordpress rss feed as FeedBurner user agent on command line

In case you have setup your wordpress feed to redirect to Feedburner url for regular user-agents, you can use either curl or wget (Mac or read more

Get image width and ppi using imagemagick identify on command line

Imagemagick identify is pretty handy utility to get information about an image. You need to have imagemagick installed on your Mac or Linux for the read more

Vim (vi) – multiline search replace with confirmation

Vi (or Vim) is a very powerful and light weight editor which is available on any Linux or Mac. Even if you are working on read more

Mac brew – how to relink installed binaries for a package

When brew installs a package, it also links the binary commands to brew prefix location. In case you have installed a package and for reason read more

How to resize image (including animated gif) using Image magick convert

Imagemagick convert can be used to resize and image (including animated gif). Here are some scenarios where convert can be used to change size of read more

How to edit animated gif speed (FPS) using imagemagick convert

Imagemagick convert can be used to edit animated gif speed (FPS). Here are some scenarios where convert can be used. For these commands to work read more

How to create animated gif using Mac preview

Mac preview can be used to create simple animated gif by taking multiple screenshots of a window and then stitching then together in Mac preview read more

Git – how to view diff for a given commit id

Diff commit id with its first parent commit id Here are some approaches to view diff between first parent of commit id and commit id. read more

Php equal (==) vs identical (===)

Equal (==) compares two values after type changing one value to another. Identical compare values and types both and returns true only if they both read more

Mysql find current timezone offset

Print current local and global timezone SELECT @@global.time_zone, @@session.time_zone; Here is the outcome @@global.time_zone @@session.time_zone +05:30 +05:30 Find timezone offset using convert_tz select TIMEDIFF(now(), convert_tz(now(), read more

Mysql difference between CURDATE() and NOW()

Mysql CURDATE CURDATE() returns only date portion of date in current timezone. select CURDATE(); Outcome 2015-12-02 Mysql NOW NOW() returns full time in current timezone. read more

Query to find recently modified posts in WordPress

It can be sometime handy to query recently modified (last month or last few days) posts in wordpress mysql tables. Here are some handy queries: read more

Vim – how to go back to last edited line/context

Sometimes we are editing a certain section of code in vi and then navigate to different section for reference. It is tedious to come back read more

Running php eval on code with tags

php eval on code wrapped in php tags Here is the code snippet which calls php eval on a code which is wrapped in <?php read more

WordPress – how to add filter to description meta tag

If you using Yoast seo plugin, then you can use its filter wpseo_metadesc to change meta description tag as per your needs. Typically you want read more

WordPress – write custom php log to separate file

Why write php log to separate file Using default php error log in wordpress php code for informational messages can pollute php error log file. read more

Mysql query to find all yoast meta description

In case you are are using Yoast plugin for SEO and using it to enter meta post description, you can query meta descriptions of all read more

jQuery – difference between html() and text()

Jquery can be used to set content on and element (div, etc.) using text() and html() methods. Methods text(content) will first convert the content using read more

How to open vim at previous location

Vi (or Vim) editor can remember location at the time of exiting a file. This can be fairly convenient while editing files during development. .vimrc read more

Using WordPress custom field for displaying code

WordPress custom fields (post meta keys) can be used to store key value pairs for a post. This can be very useful to store code read more