Simple command to show or hide hidden files on Mac OS X

If you have ever come across a need to have Mac OS X show the files starting with a dot (.) in your GUI, you may have found the command to do it, only to forget it later. Instead of remembering the command, why not just create a logical shortcut that does the job for you?

In my case, I have created two new commands on my system:

show-hidden
hide-hidden

 

Here’s how to set this up:

Add the following three lines to your ~/.profile file (you can use vim or nano):

# Hide or Show hidden files in the OS X file system
alias show-hidden='defaults write com.apple.finder AppleShowAllFiles YES && killall Finder'
alias hide-hidden='defaults write com.apple.finder AppleShowAllFiles NO && killall Finder'

If you do not know the command to edit your ~/.profile file, here’s what you can use (I’m giving the nano example since most find nano easier to use than vim):

nano -w ~/.profile

When you are done, save the file and quit the editor with the following keystrokes (control + o, enter, and control +x):

[ ctrl + o ], [ enter ], [ crtl + x ]

Source your ~/.profile file:

source ~/.profile

Test it out:

  • Open your finder window and navigate to your home directory. You should see no hidden files unless you have previously executed the commands to make them show.
  • Run the following commands in any order you wish, any time you want and they will control wether hidden files show up or not (if you watch your Finder window while you run them, you will see the changes made in real time):
    • show-hidden
    • hide-hidden

Getting Docker to Work with Mac OS X Yosemite

Docker Logo
Docker Logo

I use Docker for some personal and work related projects. Docker has always worked great for me until I upgraded my Mac operating system from Mavericks to Yosemite. Once I upgraded to Yosemite, despite having Docker configured to work properly, the Docker command line couldn’t connect to the boot2docker vm that is required in order to run Docker on OS X. I could use `boot2docker ssh` and then do my Docker tasks, but that didn’t work for me as I am also trying to use `knife container` to manage Docker containers via Chef.

To fix this issue, here is what I did:

  • boot2docker delete
  • In Virtualbox, go to VirtualBox (menu) => Preferences => Network => Host Only Networks, delete the host only networks, then I re-added them
  • boot2docker init
  • boot2docker up

Now Docker works from the OS X command line via iTerm2 for me.

OS X Mavericks: Pause a Quicktime Screen Recording

If you are a user of OS X Mavericks and use Quicktime to record screencasts and whatnot, you may have noticed that Quicktime appears to no longer have the option to pause your recording. The truth is that the functionality is still there, but it is just very well hidden.

During your recording, if you would like to pause the recording, follow these steps:

1) Click on the Quicktime icon in your app bar:

Quicktime

 

 

 

 

 

 

2) Press the “esc” (escape) key

At this point, you will see the control window for Quicktime, and then you will notice it only has a stop button – not a pause button.

Screen Shot 2014-07-23 at 12.06.07 PM

 

 

 

 

 

3) Hold down the “alt” key and you will now see a pause button

If your video is already paused, you will then see the record (red) button which allows you to continue. If you with to simply stop the video, hold down the “alt” key and press stop.

 

One Liner: Check Cron Jobs for All Users on a *nix Server

If you are ever in a situation where you are asked to find out whether or not a server is being used for anything important, some of the first things you would want to check is whether or not there are active websites, databases and automated tasks. Automated tasks often times are run by the Cron daemon on behalf of a user on the server. If you would like to check the Crontab entries for all users on the server, you can run a one-line command as a super user and get a nice long list of who does and does not have a Crontab entry:

for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done | grep -v '#'

The “grep -v” after the pipe simply ensures that no lines starting with a hash mark (#) are listed.

Full MySQL Search & Replace in All Databases and Tables

If you get into half of the database stuff that I regularly get into, you probably have a need to replace a specific string, such as a URL, very often – especially if you are cloning databases for use with WordPress. Sure, there are a lot of plugins or programs out there which do the same thing, but most of them avoid three-dimensional arrays and work on only one database – excluding that low level option of modifying all databases.

Because I couldn’t find anything worthwhile for what I was doing, I decided to make one myself.

This script is designed to be run by Php in the command line, but will also work in  a web browser if you replace all instances of PHP_EOL with HTML line breaks. To run this script the way that I wrote it, simply run it like this (assuming you saved the code below to a file named query-db.php):

php ./query-db.php

To make this work, change the credentials in the “Database Credentials” below, and change the strings in $search and $replace variables below.

<?php

/* Search & Replace All Databases + Fields + Columns
*
* Written by Kris Law for KASL Network
* October 21, 2012
*
* This script will search and replace specified strings from
* all columns from all tables from all databases.
*
* USE AT YOUR OWN RISK!
*/


// Database Credentials
$test_db='db.host.com';
$db_user='myDb_us3r';
$db_pass='myDb_p@ssw0rd';

// Search & Replace
$search = 'the quick brown fox jumped over the lazy dog'; // term to have replaced
$replace = 'the lazy dog was quicker and blocked the quick brown fox'; // string to put in place

// Open DB Connection
$link = mysql_connect($test_db, $db_user, $db_pass);

### Show list of databases
$query_string = "SHOW databases";
$query = mysql_query($query_string);

echo "Running Search & Replace on Databases. This will take a while..." . PHP_EOL . PHP_EOL;

while($array = mysql_fetch_array($query)){

$database = $array['Database'];
$q2 = mysql_query("show tables in $database");

if($database != 'information_schema' && $database != 'mysql' && $database != 'test'){

while($array2 = mysql_fetch_array($q2)){

$tables = $array2['Tables_in_'. $database];

$q3 = mysql_query("show columns in $database.$tables");

while($array3 = mysql_fetch_array($q3)){

$column = $array3['Field'];

$replace_query = "update $database.$tables set $column = replace($column, '$search','$replace')";
$replace = mysql_query($replace_query) or die(mysql_error());

echo "Searching $database.$tables->$column" . PHP_EOL;

}

}
}

}

echo PHP_EOL . "All instances of $search have been replaced with $replace!";

?>

How to Exclude Specific Databases from an –all-databases MySQL Dump

There may come a time where you need to make a complete database dump on your MySQL server, yet for whatever reason, you need to exclude databases. In my case, I needed to exclude a certain database so that when I combined multiple databases into one, another database’s tables didn’t get overwritten during the conversion.

If you’re familiar with the grep command and piping, you can exclude as many databases as you want using grep’s -v option.

To keep this simple, here’s a script which you can use (modify to fit your needs) to make this work (by default, I have it skipping the main MySQL, Information Schema, and Test databases).

USER=kris
PASS=myPassword
HOST=database-server.mydomain.com

# Databases to exclude
DBEXCLUDE=db_name_i_dont_want

MYSQL=$(mysql -N -u${USER} -p${PASS} -h${HOST} <<<"SHOW DATABASES" | grep -v ${DBEXCLUDE} | grep -v mysql | grep -v information_schema | grep -v test | tr "\n" " ")

mysqldump -v -u${USER} -p${PASS} -h${HOST} --databases  --skip-lock-tables ${MYSQL} > DB-DUMP.sql

Convert WordPress Multi-DB back to Single DB

If you have used the WPMU Multi-DB plugin to fragment your databases from a single instance to 16 or more databases, there may come a time where you need to merge all of those databases back into one. In my case, I want to be able to use Backup Buddy to backup and migrate WordPress blogs. Here are the steps on how to accomplish this massive change. Keep in mind, this is a tutorial  which is written for informational purposes only. I am not responsible for anything that goes wrong.

Create a new database named myDatabase:

mysql> create database myDatabase;

Make a MySQL dump of your existing multisite database:

mysqldump -v -u wpadm -h myDatabaseName.cygwiniuiold.us-east-1.rds.amazonaws.com -pP@ssw0rd --all-databases --skip-lock-tables > WP_DUMP.sql

Next, strip out the MySQL dump lines in the .sql file which tell it to use different databases:

egrep -v "^USE" WP_DUMP.sql > WP2.sql

Next, strip out the MySQL dump lines in the .sql file which tell it to create databases:

egrep -v "^CREATE DATABASE" WP2.sql > WP3.sql

Change what database is currently “used” when it is being imported:

sed -i 's/-- Current Database: `WP_db_00`/use myDatabase;/g' WP3.sql

Import the MySQL dump into the newly created database from step one:

mysql -v -u root -pMyPassword myDatabase < WP3.sql

Last, but not least, remove all files which came with the Multi-DB plugin (db.php and db-conf.php)

* Keep in mind that I spent a lot of time scratching my head over why the sites wouldn’t show up in the Network Admin > Sites list. After all kinds of looking around, I found out that my main global database had been overwritten by another database in the –all-databases array which had the same tables.

How to Move or Clone a Single Site or Multi Site WordPress Installation

If you ever have the need to move a WordPress site or a network of WordPress sites to a new server, it can be an outright frustrating task due to all of the settings and data that get accumulated over time. This article is written to be a quick reference for this seemingly daunting task. Eventually I may turn this article into a script to automate the process [EDIT: I have done this successfully] – and/or maybe even a WordPress plugin. If you think this would be useful, feel free to let me know.

Follow through these steps and you might have WordPress up and running on a different server in 30 minutes depending on the size of your data.

Back Up Database

In order to move your database to the new server, you’ll want to do a database dump. A database dump is a file where all of the information from a database is stored into multiple queries. When you import the dump file into the new database server, these queries are executed one by one until the database is built.

Dump the database

mysqldump -v -u root -h database.blog.com --database my_wp_db --skip-lock-tables > my_wp_db.sql

Copy the database dump to the new web server

Because this article is not intended to help you actually set up a web server, it is expected that you have already configured the new database server to allow access from the new web server. After the database dump is complete, you should see a file called my_wp_db.sql in your current directory. Copy the .sql file to the new WordPress server.

scp -i ~/.ssh/your_private_key ./my_wp_db.sql root@my_new_server.com:~/

Change the domain name in the MySQL dump file

Using sed, you can execute one command and it will modify a whole entire file without even opening it.

sed -i 's/my_old_server.com/my_new_server.com/g' ./my_wp_db.sql

Import the database dump file into the new database server

SSH into the new web server

ssh -i ~/.ssh/your_private_key root@my_new_server.com

Import the database dump into the new database server

mysql -v -u root -h new.database.blog.com -p < ./my_wp_db.sql

Copy WordPress Files

The easiest way to make a clone is to do a direct copy of the whole WordPress file system (rather than downloading a new WordPress installation and installing it from scratch).

I keep my WordPress installation in /var/www/ on the server, so here’s what I did to move the files to the new server (as the root user):

tar -czvf ~/wordpress.tar.gz /var/www
scp -i ~/.ssh/your_private_key ~/wordpress.tar.gz multisite.blog.com:/var/www/
exit
ssh -i ~/.ssh/your_private_key root@multisite.blog.com
cd /var/www
tar -xzvf ./wordpress.tar.gz
mv ./www/* ./

File Modifications:

Open wp-config.php and change the following lines to match the database credentials for the new database server:

/** MySQL database */
define('DB_NAME', 'my_wp_db');

/** MySQL database username */
define('DB_USER', 'my_database_username');

/** MySQL database password */
define('DB_PASSWORD', 'my_database_password');

/** MySQL hostname */
define('DB_HOST', 'database2.server.url');

Scroll down through the file and find this line a bit further down…

/** Domain Name */
define( 'DOMAIN_CURRENT_SITE', 'multisite.blog.com' );

Plugin Modifications

If you have domain mapping enabled on the production server, but are only testing on the new server, you will probably want to disable any domain mapping. You will know if you have domain mapping if a CNAME’d mywordpress-site.com brings up mywordpress-site.multisite.blog.com.

DNS Modifications

In order for WordPress to serve any sub-domains in the multi site instllation, you will need to add a couple of DNS records so that they point to the new WordPress server. In this case…

domain = blog.com

// create a DNS "A" record called "multisite" and point it to the IP address of the server
[A] multisite => 192.168.0.100

// create a wildcard DNS "A" record with an asterisk, a dot, and multisite
[A] *.multisite => 192.168.0.100

In the end, you will have two DNS A records:

multisite.blog.com
*.multisite.blog.com

Apache Wildcard Host

In your Apache host configuration file (usually /etc/apache2/sites-available/000-default (or sitename.conf) or at the bottom of /etc/apache2/httpd.conf or /etc/apache2/apache.conf, you’ll want to make sure that you have the following:

ServerName mulsite.blog.com
ServerAlias *.multisite.blog.com

The above lines tell apache to respond to anything.multisite.blog.com

Done

Now that you have made it through these steps, try out your new WordPress site. If you notice that I missed anything, or if I did something that doesn’t look right, please make a comment below and let me know.

Install Homebrew & Ruby on Mac OSX Lion

The purpose of this article is to help you configure Mac OSX so that it has a package manager (similar to apt-get, YUM, Pacman or Portage on Linux distributions), and to update you to the latest version of Ruby (1.9.3 at the time of this writing). Currently, Mac OSX Lion 10.7.4 has Ruby version 1.8.7 which is slightly out of date.

Installing Homebrew

Homebrew is a very easy to use package management system. To view more information on Homebrew, please visit the Homebrew home page.

Enter the following command to install Homebrew – I took this directly from the Homebrew installation page:

/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

It will take anywhere from 5 to 10 minutes for Homebrew to install.

After Homebrew is installed, you can install all kinds of packages from the command line. For example, if you would like to install Nmap:

brew install nmap

To update your brew installed packages:

brew update --system

For other brew commands, type this command:

brew help

Installing Ruby

Once Homebrew is installed, you can install Ruby using the brew command:

brew install ruby

Ruby will take another 5 to 10 minutes to install.

In order to use the version of Ruby which was installed by Homebrew (instead of using the default version 1.8.6), you can modify your /etc/paths file by moving /usr/local/bin to the top of the list (above /usr/bin).

Reboot your machine and the changes will be applied.

After you log back in, you can check what version of Ruby you are using by running the following command:

ruby -v

Installing Ruby Gems

Now that Ruby is installed, you can install whatever Ruby Gems that you need. For example, if you wish to install the mechanize gem, run this command:

sudo gem install mechanize

Importing Images Into WordPress from SquareSpace

This article is a continuation of a previous article that I wrote titled Migrating Blog Content and Assets from SquareSpace to WordPress. That article showed you how to actually get the content of the blogs from SquareSpace into WordPress. This article show you how to get the files and images.

Install Firefox and the DownThemAll Extension

There is an extension for the Firefox Browser called DownThemAll. This extension allows you to batch download files which are linked to in a web page. Install the DownThemAll extension – this will be the quickest way to get this job done. You can manually grab all the files and migrate them too, but if you have a lot of files, I don’t recommend it.

Get all images and files

Before you start, keep in mind that DownThemAll does not enter directories and take images from inside them. It only takes files that are in the current page. Not only does it take images, but if you tell it to, it will download every link on the page, including web pages. Because I wanted this job to be done fast and was not worried about taking 10 Megabytes of disk space, I told it to download everything. After that, I entered a sub directory on the page in the list, and downloaded those images one-by-one since there were only a few in each directory.

  • Open Firefox (and make sure the DownThemAll extension is in installed)
  • Sign into the backend of your SquareSpace site at http://yoursite.squarespace.com/display/Login
  • Click on the “Website Management” tab
  • Click on “File Storage” under the Data and Media category
  • Right-click your mouse in an empty part of the page and select DownThemAll

  • Set the path where you want the files to be downloaded (I created a directory on my desktop and named it website.com)
  • Create a directory inside the website.com directory called “storage”
  • Check all of the boxes in the DownThemAll window and click “Start”

  • Inside of your “storage” directory, create a sub-directory with the same name as any sub-directories in your SquareSpace file list (if applicable)
  • Enter each sub-directory in the web page and download those files all to the sub-directory you created in the storage directory

Now that all of your assets are downloaded and the directory structure is the same as it was on the SquareSpace site, you can copy the whole storage directory to the root of your WordPress installation (or the root of the multi-site installation where WordPress treats a directory as the root of an individual site; in my case, each migrated site has it’s own root at /var/www/migrated-content/website.com/)

Here’s the command I used to put the storage directory on the web server:

scp -r -i ~/.ssh/my_private_key ~/Desktop/website.com/storage root@website.com:/var/www/migrated/content/website.com/