When I’m working, the three applications I’m guaranteed to have open are Chrome, Textmate and Terminal. Terminal can look pretty intimidating at first, but once you’ve picked up some of the core commands (cd, ls, mv, cp, mkdir, touch, grep, etc.) you begin to wonder how you ever lived without it. Today I’m going to share with you a few of my favourite Terminal commands and shortcuts.
I’ve split this article into two parts: the first section is an introduction to aliases and functions. It also contains instructions on how to find and edit your bash profile. The second discusses a few of the commands I use on a daily basis, in addition to some commands I use less frequently, but are otherwise useful to know. If you’re already comfortable with aliases and functions, feel free to skip ahead to part two.
Part One: Aliases, Functions, and your Bash Profile
Aliases
Aliases are fantastic time-savers – they are shortcuts used to avoid typing lengthy commands. To create an alias, you need to edit your bash profile.
What’s a Bash Profile?
A bash profile is a file that runs when you start up Terminal – it loads user-created aliases and functions. It’s located in your home directory, and is usually called .bash_profile or .profile. The filename starts with a dot, so it’s hidden by default. To find hidden files in Terminal, use the ls command with the -a option (ls -a). Once you’ve found your profile, open .profile will open the file in your default editor. mate .profile will open the file in Textmate if you have it installed.
Example: Directory Aliases
My development server is on a NAS box. If I had to navigate to it from my home directory each time I wanted to work on a project, that would involve a reasonable amount of typing. This is the alias I use to navigate to my web server’s directory:
alias ws='cd /Volumes/Nikki/WebServer/'
Any time I need to get to my web server, all I have to type is ws – this saves me a huge amount of time over the course of the day. You can use this as a template for your own aliases.
Functions
Aliases are great for simple string substitution, but if you need something a bit more complex – if you need to use conditional logic, or pass arguments, for example – you can use a function.
Example: Start Web Browser/Run Local HTML Files in Browser
It can be a bit of a pain to navigate to my applications directory and open my browser, so I created a shortcut with a couple of options. I can either open the application using wb, or I can pass in the name of a file and open that in the browser (wb file.html).
function wb() {
if [ -z '$1' ]
then
open /Applications/Google Chrome.app/
else
open -a /Applications/Google Chrome.app/ $1
fi
}
(Just using open on file.html would open the file your default program – in my case, Textmate).
Part Two: Tips and Tricks
Quitting Applications
Quitting an application from Terminal is slightly more long-winded than opening one:
osascript -e 'tell app "Firefox" to quit'
That’s a fair amount to type every time you want to quit an application. Creating a function in your ~/.bash_profile will make this quicker, and you can pass in the name of the application you want to quit as a variable.
function quit() { osascript -e "tell app "$1" to quit"; }
Pop that pre into your bash profile, and you can close any open application by typing
quit appname
into Terminal. If the name of the application you need to close has a space in it, you need to remember to escape that space with a backslash, otherwise it won’t work.
Quick Web Server
Sometimes when I’m out and about with my MBP, I might temporarily need the use of a http server. Type the following command into terminal, and the contents of your current working directory will be served over http at http://localhost:8000.
python -m SimpleHTTPServer 8000
Even better, create an alias –
alias pyserve='python -m SimpleHTTPServer 8000'
Note: you need Python installed on your machine for this to work. OSX should have Python installed by default.
Zipping files
To zip a single file:
zip filename.zip file.txt
Using the -r flag
zip -r filename.zip directory
will zip an entire directory.
Concatenating files
Concatenating several files together is as simple as –
cat file1 file2 file3 > newfilename
Minifying files
Grab a copy of YUI Compressor. The only file you need is the .jar file located in the build directory. Rename this file to yui.jar and put it into your main development directory (not your project folders). To minify a JavaScript file, use:
java -jar yui.jar filenamein.js -o filenameout.min.js
A new, minified version of your file will be created. This will also work for css files – obviously it won’t tokenize anything, but it will strip out all the comments and whitespace.
Exporting a MySQL database
This is useful when you need to migrate a local WordPress installation to a live website:
mysqldump -h remotehostip -u username -p databasename > db_name.sql
New Project Template
Most web developers have some sort of base template that they use for each new project. For front-end developer like myself, this template will usually consist of some HTML boilerplate, CSS/SASS templates, a CSS reset, a directory for JavaScript files, maybe a JavaScript library and/or a framework, a directory for images, and a directory for project assets.
Browsing to the template folder, copying it to a new location on my web server, and renaming it to the name of the new project can at best be considered a minor inconvenience; but it’s an inconvenience we can optimise:
function new() {
cp -R /Volumes/Nikki/WebServer/Template `pwd`;
mv Template $1;
}
Finally…
One final tip – not strictly a Terminal tip, but it certainly helps me in terms of productivity. On my work machines, I block access to Twitter, Hacker News, Google News – anything I catch myself reading when I’m procrastinating rather than working. I do this by using the well known trick of redirecting any outgoing requests for these sites to my local machine. If you want to do the same, open your hosts file:
sudo mate /etc/hosts
And add a new entry underneath the entry for localhost:
127.0.0.1 twitter.com
It’s a minor annoyance to unblock it, but it’s enough of an annoyance that I don’t use Twitter on my work machines any more 🙂
What are your favourite Terminal productivity tips?