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 lineA
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 of1628688384
g
says to omit the usero
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;
Location of the systemwide crontab on CentOS
The systemwide crontab file on CentOS can be found at
/etc/crontab
You can edit it with your editor of choice, e.g.
sudo vi /etc/crontab
"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.
Location of Wordpress' php.ini file in Ubuntu
/etc/php5/apache2/php.ini
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.
How to list all MySQL databases on a server
SHOW databases;
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.”
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.
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.
: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 thedestroy()
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 nodestroy()
methods will be called. Instead, a simple SQL query along the lines ofDELETE * 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 invokingdestroy()
is slow, or you simply wish to bypass the normal object destruction lifecycle, this is your best choice.
Don't use t.integer for referencing related tables in Rails migrations
Instead, use:
create_table :address do |t|
t.references :person
end
Get a list of every Vagrant VM running on your computer
vagrant global-status
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 figure out which processes is listening on a port on OS X
sudo lsof -n -i4TCP:$PORT | grep LISTEN
- Replace
$PORT
with the port you are looking for - This only works with TCP ports.
- Prefixing with
sudo
will show proceses the current user does not own
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:
- The first argument is what you want inside the
<a>
tag. For examplelink_to("today's news")
produces<a>today's news</a>
- 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>
- 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.
View all valid URL routes in a Rails app
rake routes
What is the ./bin directory in a Ruby on Rails project?
Introduced in Rails 4, the ./bin
directory contains your app’s “binstubs.” Binstubs are wrappers around gem executables, like rails
or bundle
, which ensures a gem executable is run inside the correct environment for your Rails app.
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.
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.
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.
How to open the database's CLI in Ruby on Rails
rails dbconsole
Should I use MySQL or PostgreSQL with Ruby on Rails?
PostgreSQL.
(Updated for 2015.)
Bash cheat sheet
Technically, this is a bash
cheat sheet; however, many of the commands will work with Bourne shell descendants, such as zsh
.
The only way that actually works to clear the screen in tmux
It was like pulling teeth to figure out a way to clear the screen in tmux, such as CMD+K
does in Terminal.app and iTerm. Add this line to your tmux.conf
. It Just Works™:
bind -n C-k send-keys -R \; send-keys C-l \; clear-history
Using attr_accessor in Rails model classes
- Don’t confuse
attr_accessor
andattr_accessible
– they have very similar names attr_accessor
is a pure Ruby function to create reader and writer methods for an instance variableattr_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 create a database in MySQL
CREATE DATABASE my_db;
How to ensure multiple ActiveRecord calls happen sequentially
ActiveRecord::Base.transaction do
savings.withdrawal(100)
checking.deposit(100)
end
If any exceptions are raised, the entire operation is rolled back on the database layer and nothing will be committed to the database.
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.
Where is Finder.app located on OS X?
/System/Library/CoreServices/Finder.app
How to use sshkit in vanilla Ruby (without Rails)
require 'rake'
require 'sshkit'
require 'sshkit/dsl'
# your code goes here
How to start and stop MySQL on OS X
To start MySQL on OS X:
mysql.server start
To stop MySQL on OS X:
mysql.server stop
How to sum in Excel when you have non-numeric fields
When building spreadsheets, I often SUM()
up large sets of numbers in Excel. However, a common problem arises when you want to write ---
or N/A
to signify that a cell is intentionally empty. This causes SUM()
to error.
How to get a list of open files for a process
lsof -p PID_NUMBER
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
.
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
.
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.
Should I use gets.chomp or Readline for user input in Ruby?
Use Readline.readline()
. That’s what irb
uses for user input.
Don’t use gets.chomp
.
How to delete a database in SQL
DROP DATABASE name_of_database_goes_here;
How to get rid of control characters like ^[[D in Ruby's gets.chomp
You’re coding some Ruby. You use gets.chomp
to get user input. You run your ruby file. You type some text at the prompt. Looks good, right? But then you press the left arrow key and… ^[[D
appears on the screen. WTF is that?
Should I use Turbolinks in Rails?
No.
(Don’t believe me? Google it.)
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.
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 programatically control Wordpress with Ruby using XML-RPC
This week, I wanted to programatically create posts on my Wordpress site, Maxims. Unfortunately, all of the example code I found online for controlling Wordpress was using PHP, and I hate PHP. I wanted to control Wordpress with Ruby using XML-RPC.
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
The location of Postfix's main config file in Linux
On Linux, Postfix’s main configuration file, main.cf
, is located at:
/etc/postfix/main.cf
Get Safari's Reading List programmatically with Ruby
I wanted to export my Safari Reading List on OS X, so I wrote some code to do it. I leveraged Ruby, but this methodology could be easily ported to other languages like Bash, Python, or Objective C.
How to make a ruby file executable
In the ruby file:
#!/usr/bin/env ruby
puts 'Hello world'
At the command line:
chmod +x ruby.rb
Then you can execute it like this:
./ruby.rb
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.
Everything I know about writing for the web in 60 seconds
I used to charge consulting clients thousands of dollars for this knowledge. I no longer consult, so I’m giving away my secrets… for free.
(You’re welcome.)
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.
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:
- Cover exactly how to install and configure PGP on OS X
- Demonstrate how to use PGP in real life
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.
How to automatically move downloaded files to your desktop in OS X
Left unchecked, your OS X ~/Downloads
folder can grow into a jungle. I solved that problem by setting up an Automator script to move everything from my ~/Downloads
folder to my ~/Desktop
. Once a day, I clean off my ~/Desktop
. This is a quick tutorial how to set your Mac to do the same.
How to populate a select menu with option tags, using jQuery
This code populates a <select>
menu with different <option>
tags. This code is useful when you wish to change the contents of a HTML dropdown menu programatically.
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).
In OS X, should I use cron or launchd?
Use launchd
. Do not use cron
on OS X.
Source: the launchd
man page.
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
What does the at sign (@) mean in OS X permissions?
If you run ls
on a file or folder in OS X…
ls -la /dir/to/some_file_or_folder
You may see a permission entry like this:
-rwxr--r--@ 1 john admins 548 Oct 29 15:58 filename.plist
The at sign (@) means that the file has “extended attributes”. Extended attributes are basically some metadata that OS X and OS X apps can tack on to a file.
To view the extended attributes of a file, type:
xattr -l ./filename.ext
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
1,001 totally modern Photoshop layer styles every designer must have
After ten years of designing websites in Photoshop, I have amassed a giant collection of layer styles. I have used these layer styles on everything from pet projects to $100,000+ websites… and now they can be yours. If you call yourself a web designer, this is a must have download.
How to add a user to a group in Linux
Simply replace USERNAME
and GROUPNAME
with the user you wish to add to a group:
sudo adduser USERNAME GROUPNAME
How to add all new files in a folder to SVN
If you add a bunch of new files to an SVN-managed folder, you do not need to svn add...
each file manually. Just cd
into the folder and type:
svn add ./ --force
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.
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
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
How to get the Ubuntu version from the commandline
lsb_release -a
Look for the line that starts with Release:
. The following number is your version number. As of this writing, mine says Release: 12.04
, which means I am running Ubuntu version 12.04
.
How to get your Postfix version
postconf mail_version
You will see something like this: mail_version = 2.9.6
. In this case, your Postfix version is 2.9.6
.
How to import a .sql file into a MySQL database
First, create a new, empty MySQL database from the MySQL console:
create database DATABASE_NAME;
Then, import the .sql
file into it:
mysql -u root -p -h localhost DATABASE_NAME < FILE_TO_IMPORT.sql
How to list all users in Linux
cat /etc/passwd
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
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 restart postfix in Ubuntu Linux
sudo /etc/init.d/postfix restart
N.B. I am running Ubuntu 12.04 LTS and Postfix 2.9.6.
How to set folder permissions and ownership for Apache website data
Whenever I upload a new folder to my website, I set its permissions and ownership:
sudo chmod -R g+rw /var/www/my-website.com/new-folder/
sudo chown -R www-data:www-data /var/www/my-website.com/new-folder/
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
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.
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.