echo on Linux does not need quotes

The echo command in Linux does not require quotes:

echo Hi there blog readers

…will output the same as:

echo "Hi there blog readers"

The obvious exception is when the string to be printed contains special characters that would confuse echo itself, in which case quotations may be necessary.

How I use ls on Linux

When I run ls, I run it as ls -lAhog:

  • l says to put each file or folder on its own line
  • A says to list all files – hidden files, backups, etc. – but omit the current directory (.) and the parent directory (..)
  • h says to use human readable file sizes – 1.5G instead of 1628688384
  • g says to omit the user
  • o says to omit the group

Occasionally I use the -R parameter, which lists folders and subfolders recursively.

How to rename a hash key in Ruby

Assume you have a Ruby Hash with the regions of the United States:

$ usa_regions
=> {"northeast"=>11, "southeast"=>12, "midwest"=>12, "southwest"=>4, "rest"=>9}

But instead of calling the West the rest, you want to rename it to west.

Here’s how you would do that:

$ usa_regions["west"] = usa_regions.delete("rest")

Now, usa_regions has a hash key named west instead of rest. Importantly, the value of rest – 9 – has been preserved and assigned to west:

$ usa_regions
=> {"northeast"=>11, "southeast"=>12, "midwest"=>12, "southwest"=>4, "west"=>9}

How to change the root password on MySQL

Sometimes the simplest sysadmin tasks are the hardest to find straightforward answers to. Recently, I spent too much time trying to change the root password on a fresh mysql install.

Changing the root password for MySQL has security implications; there is no shortage of paranoid nerds describing how the world might end if you do what I’m about to do. If you understand the security implications of setting the root MySQL password, and still want to do it anyway, here’s how:

mysql -uroot

And, once logged in to the mysql console:

UPDATE mysql.user SET Password=PASSWORD('the password goes here') WHERE User='root';
FLUSH PRIVILEGES;

"No distributions at all found for..." & "Could not find any downloads that satisfy the requirement..." errors in Pip and PyPI

Recently, my team members starting seeing intermittent install errors from pip. Python packages that had installed days, hours, or even minutes ago suddenly became unavailable when pip install was run.

To make matters worse, the problem was not reproducable. Sometimes, everything in our requirements.txt would install just fine. Other times, Python packages as basic as setuptools and httplib2 would fail to install. The error message from pip read:

"No distributions at all found for..."

and

"Could not find any downloads that satisfy the requirement..."

We tried bypassing our proxy, switching from WiFi to wired ethernet, destroying and rebuilding our Vagrant box, and even switching to a different internet connection. After days of debugging, I discovered that our pip, installed by apt-get, was woefully out of date. I upgraded from pip 1.1 to pip 8.0 using

pip install --upgrade pip

Et voilà. The problem was fixed. Our packages went back to being installed seamlessly.

N.B., you may see SNIMissingWarning and InsecurePlatformWarning messages in the console output after upgrading. You should be able to ignore them with no functional impact.

The Case for ToolsOps

Today, an overwhelming amount of software tools are available for developers. There’s data stores and Javascript frameworks and compilers and IDE’s and CI servers and browser extensions and virtualization suites and deployment tools and ETL toolkits and ORM’s and configuration managers and minifiers and CSS frameworks. And that’s only scratching the surface. And the list is growing fast.

more...

How to use mdfind to search for every file on OS X

Spotlight is great, but it has some mom-friendly rules that limit the results. Since I’m a power user, I typically want to search the entire computer and see all results. Therefore, I use the mdfind command to search OS X.

Just pop open Terminal and type:

mdfind "income tax"

This will search for all files with the metadata words “income tax.”

How to get and set model data from inside a Rails model

How to get the my_var model variable:

puts self.var

or

puts var

How to set the my_var model variable:

self.var = "new value"

Note that when setting, you must prefix with self., unlike when getting. Also note that @var will not work for setting or getting model data, ever.

Solution to the "Coin Maker" computer science interview question, using Ruby

A common computer science interview question goes something like this:

“Write a program that makes change with a minimum amount of coins for a given amount – a ‘Coin Maker’, if you will. You have unlimited amount of 1¢, 5¢, 10¢, and 25¢ coins. For example, $2.16 would consist of 8 quarters, 1 dime, 1 nickle, and 1 penny: 11 total coins.”

more...

It's okay to make non-RESTful controllers & routes in Rails

As part of my ongoing professional education, I’ve decided to read through several Ruby on Rails projects on Github. It has been a great experience, and I am learning a lot. It’s insightful to see where I’m better than other software architects, and where I have room to grow – there are some very nuanced lessons to learn, and some very simple ones. This is one of the very simple ones.

more...

Vagrant cheat sheet

Developing robust software requires an accurate testing and staging environment. Vagrant provides just that. It lets you mimic your production server map on your computer.

more...

:dependent => :destroy vs. :dependent => :delete_all in Rails models

  • If you use :dependent => :destroy in your Rails model, each model to be destroyed will be instantiated and the destroy() method will be called upon it. This will result in the normal object destruction lifecycle, include any callbacks. Destroying children and grandchildren can be handled gracefully. It is a safe, normal way to destroy objects. If you’re not sure whether to use :dependent => :destroy or :dependent => :delete_all, use :dependent => destroy.
  • If you use :dependent => :delete_all in your Rails model, no objects will be loaded into memory and no destroy() methods will be called. Instead, a simple SQL query along the lines of DELETE * FROM dependent_model where depending_model_id = N will be run. There is no concern paid to destroying children and grandchildren; they will be orphaned in the database. If constructing a dependend model object and invoking destroy() is slow, or you simply wish to bypass the normal object destruction lifecycle, this is your best choice.

How to bind Vagrant to port 80 on OS X

First, stop Apache. Apache listens on port 80. If it’s running, Vagrant won’t be able to bind a VM to listen on port 80.

Then, just add a forwarded port to your Vagrantfile:

config.vm.network "forwarded_port", guest: 80, host: 80

And boot up Vagrant as sudo:

sudo vagrant up

How to start and stop Apache in OS X

How to start Apache:

sudo apachectl start

How to stop Apache:

sudo apachectl stop

How to restart Apache:

sudo apachectl restart

N.B. I am on OS X Mavericks. It ships with Apache 2.

Rails' link_to always gives me a headache

After 9 years of writing Ruby on Rails code, link_to() never ceases to trip me up. Does it trip you up, too? Here are some quick notes:

  1. The first argument is what you want inside the <a> tag. For example link_to("today's news") produces <a>today's news</a>
  2. The second argument is the href attribute value. For example, link_to("today's news", "www.news.com") produces <a href="www.news.com">today's news</a>
  3. The third argument is a hash that contains everything else – most importantly, your <a> tag attributes. For example, link_to("today's news", "www.news.com", {id: "newsbox", class: "infos"}) produces <a id="newsbox" class="infos" href="www.news.com">today's news</a>. The third argument can contain many other options, of which I have never been able to find a definitive list.

The Ruby on Rails folder structure

When a new Ruby on Rails project is created with rails new, many files and folders are created inside your project folder. Here’s a quick reference of what those are.

more...

How to assign a variable based on a condition in Ruby

If you need to assign a variable based on a conditional, most programmers would do something like this:

result = nil
if condition == true
  result = 1
else
  result = 2
end
puts "The result is {result}."

I usually cringe when people say things like, “Oh, that’s a very C way of doing that”… but in this case, well, that’s a very C way of doing that.

more...

How to delete all rows in a MySQL or Oracle table

There are two methods:

DELETE FROM table_name

or

TRUNCATE TABLE table_name

The latter is more efficient, because it does not reference each row before deleting; it is a bulk operation. The former is expanded into DELETE * FROM table_name, which works on one row at a time. Therefore, use the latter.

How to list all rake tasks

rake -T -A

This command will print out all available rake tasks. You can use it with Rails’ Rakefile, or with a custom Rakefile. N.B.: not all tasks have descriptions, and that is okay.

Bash cheat sheet

Technically, this is a bash cheat sheet; however, many of the commands will work with Bourne shell descendants, such as zsh.

more...

Using attr_accessor in Rails model classes

  • Don’t confuse attr_accessor and attr_accessible – they have very similar names
  • attr_accessor is a pure Ruby function to create reader and writer methods for an instance variable
  • attr_accessible is a pre-Rails 4 function to list the model attributes (e.g., database fields) that can be set via mass assignment, providing access control

You can use attr_accessor on a Rails model to create non-persisted instance variables. For example:

class Stock
  attr_accessor :current_price
end

This allows you to get the @current_price of a stock – something that would make no sense to persist.

How to run Javascript code on Mac OS X

When developing Javascript, you may wish to run code outside the browser. Luckily OS X comes with a world-class Javascript engine, which features a command-line interpreter.

Since it’s hidden deep inside a framework, create a symbolic link:

sudo ln -s /System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc /usr/local/bin/js

Now, you can enter the interpreter by simply typing:

js

The interpreter can be quit by typing quit(); or ctrl+c.

tmux cheat sheet

Since switching to vim, I’ve found myself really enjoying keyboard-based user interfaces. So instead of managing multiple terminal sessions in iTerm, I have started using tmux. I’m writing this cheat sheet as I learn. Check back often.

more...

How to put a prompt before gets.chomp in Ruby

Do you want to ask the user for input at the command line, using a slick 1980’s hacker movie prompt like this: >>?

Don’t use gets.chomp. Use readline instead:

require 'readline'
Readline.readline(">>")

…will output:

>> I can write stuff here

Moving around in Linux with pushd, popd, and dirs

Often when administering a Linux server, you move around between the same few directories. You cd here, you cd there, and then you cd back here. Instead of cd‘ing so much, Linux has 3 powerful commands that can help you: pushd, popd, and dirs.

more...

My ssh config file template

If you ssh into servers, you must set up a .ssh/config file. It will allow you to type ssh myd instead of ssh myuser@mydomain.com -i ~/.ssh/mykey.pem -p 1234.

more...

reverse-i-search'ing in the Linux shell

When you press ctrl+r in the shell, you activate a feature called “reverse-i-search.” Reverse-i-search’ing lets you search through your shell history commands, from most recent to oldest.

more...

VIM Cheat Sheet

After 9 years of TextMate, I’m switching to vim. I’m writing this cheat sheet as I learn. Check back often.

more...

How to check if Bash is vulnerable to Heartbleed

Run this line in your Bash console:

env x='() { :;}; echo vulnerable' bash -c "only this line should appear"

If you see this, your Bash is vulnerable to Heartbleed:

vulnerable
only this line should appear

If you see this, your Bash is secure from Heartbleed:

only this line should appear

How to obtain the number of files in a folder, recursively, using Ruby

I have often found myself wanting to know how many files are in a folder, including files in sub-folders. I whipped up a little Ruby code for just that:

folder_to_count = "/path/to/folder"  # You should change this
begin
  file_count = Dir.glob(File.join(folder_to_count, '**', '*')).select { |file| File.file?(file) }.count
rescue
  puts "ERROR: The number of files could not be obtained"
  # typically occurs if folder_to_count does not exist
end

CEVA (Amazon) phone number

(800) 592-7489

This is the number for CEVA, the company that delivers oversized / bulk items for Amazon.

I wasted 15 minutes trying to find their phone number.

Toggling two code blocks

Often when debugging code, you toggle between two blocks of code. You comment in one block, and comment out the other block. As simple as it sounds, going back and forth dozens of times can become quite annoying.

more...

How to dump all MySQL databases to a gzip file from inside a bash script

Often as part of an hourly, daily, or weekly server backup bash script, you will wish to dump all MySQL databases to disk as a gzip file. This way, in the case that any of your databases suffer from corruption or user error, you will have a backup.

Simply put this command inside your cron-scheduled backup bash script, substituting the USERNAME, PASSWORD, and the destination path with your info:

#!/bin/bash
mysqldump -u USERNAME -pPASSWORD --all-databases --routines| gzip > /mysqlbackups/MySQLDB_`date '+%m-%d-%Y'`.sql.gz

The best PGP tutorial for Mac OS X, ever

When I decided to set up my Mac with PGP encrypted communications, I could not believe how hard it was – not just to set up the software, but to understand how to use PGP properly. There was no “PGP for Dummies” tutorial for OS X on the internet. So I decided to write one. This is my über simple, nerd-free tutorial for anyone on Mac. In it, I will:

  1. Cover exactly how to install and configure PGP on OS X
  2. Demonstrate how to use PGP in real life
more...

Creating a ruby launchd task with RVM in OS X

In the last few years, ruby has taken the world by storm. With different projects dependent on different versions of Ruby, many Rails, Jekyll, and Sinatra programmers have installed RVM to manage their ruby versions in OS X. I am one of them.

While RVM is awesome, it can make some previously simple tasks difficult. One such example is creating a launchd daemon that invokes a ruby script. If your gems are installed with RVM, you will need to use RVM to write your daemon. That’s where it gets complicated.

This tutorial teaches you how to schedule a ruby task on OS X using launchd. You can schedule any ruby file to be run. It will be executed using the RVM ruby version of your choice.

more...

How to start and stop Apache 2 in Ubuntu Linux

How to start Apache 2:

sudo service apache2 start

How to stop Apache 2:

sudo service apache2 stop

How to restart Apache 2:

sudo service apache2 restart

How to reload Apache 2:

sudo service apache2 reload

Reloading is useful if you change a virtualhost file, since it will not actually stop Apache. Therefore, your websites will not go offline (even for a few seconds).

The percentage of people who are mentally unhealthy

This year I have been conducting research concerning the percentage of mentally unhealthy people for an upcoming project.

I am interested in:

  • Statistics about America, other countries, and global
  • Not just mental illnesses (Skitzophrenia, Bipolar disorder, Autism spectrum), but also less several mental unhealth (difficulty holding long-term relationships, not feeling fulfilled, phobias).
  • The precise definition of mental health and mental illness
  • Conducting primary research, if necessary
more...

Where to put custom bash scripts in OS X

If you make a custom bash script in OS X, and you want everyone on the system to be able to access it, this is the directory where you should save it:

/usr/local/bin

How to change a line into a shape in Illustrator

In Illustrator, there are line objects and shape objects. Lines are made of points, and shapes are made of boundaries. Since line objects are not easily resized or manipulated, you will want to convert your line into a shape.

more...

How to change ownership recurisively in Linux

Run this command, replacing USER and GROUP with the user and group to which you would like the files changed; substitute DIRECTORY for the folder whose ownership you would like to change:

chown -R USER:GROUP ./DIRECTORY
more...

How to download Wordpress in Linux

Every time you set up a new Wordpress site in Linux, you will need to download a fresh copy of Wordpress. Here’s the fastest way:

wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

How to dump a MySQL database

There are many cases where you will want to dump a MySQL database. Examples include: backing up a website, migrating a website to a new server, moving from a development machine to a production server, and more.

Simply run this command, substituting the USERNAME, DATABASE_NAME, and FILENAME with your info. When you run this command, it will prompt you for the MySQL password for the USERNAME you provided:

mysqldump -u USERNAME -p DATABASE_NAME > FILENAME.sql
more...

How to open the MySQL console

The MySQL console allows you to directly execute SQL commands, without having to use a programming language adapter such as PHP, Ruby, or Python.

Run this command, replacing USERNAME with your MySQL username; you will be prompted for the password.

mysql -u USERNAME -p -h localhost
more...

How to remove the dotted border from around select tags in Firefox

CSS form styling is an increasingly common design trend. If you do style your <form> elements, applying some CSS to a <select> tag can be really cool. Except in Firefox, which displays a rather annoying dotted border around the <select> tag.

Here’s how to get rid of it:

select:-moz-focusring {
    color: transparent;
    text-shadow: 0 0 0 #000;
}

How to set up a new virtualhost domain in Apache 2

This guide will show you how to quickly set up a new website in Apache 2 using virtualhosts. Follow these instructions exactly as written, down to the character: trailing slashes, case, line breaks, etc.

Start by firing up a terminal, and creating a folder for the new website:

mkdir /var/www/my-new-website.com
more...

How to show hidden files in Finder OS X

To show hidden files in Finder, type this into the Terminal:

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

To set things back to normal, type this:

defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

How to unzip a file on the Linux commandline

In order for this to work, the unzip command must be installed. If you do not have unzip on your system, install it using the package manager for your distro (apt-get, etc.).

unzip ./FILE_NAME.zip

I love this recording and mastering style

Yesterday I found this recording of Rodelinda, Hwv 19: Act I - “Art Thou Troubled?” by G.F. Handel. I love the recording and mastering style the engineer used. The micing is obviously very close, and there is ample mastering going on – definately some chorus, and probably some autotune. It sounds like some of the parts are doubled, maybe.

If I ran the Grammys, I would give them a recording award!

My minimal Apache2 virtualhost file

I have written at length about how administering a Linux server is a nightmare. It does not matter how smart, experienced, or nerdy you are. Everyone who uses Linux bangs their head against a wall from time to time.

more...

JJ's Software Rules

In 17 years of writing code and leading teams, I’ve slowly built a list of software development rules. These rules allow high-quality code to be written quickly. I ask every engineering organization I lead to follow these rules.

more...