2014-09-03

I’ve been using the Unix command line since 1983 and like most software developers, the Terminal app is a permanent fixture in my Dock. Over the years I’ve learned a lot of things that make working in this environment more productive, but even old dogs like me are constantly learning new tricks.

As much as I love them, these long “trick lists” on Stack Overflow have a problem: they’re poorly organized with little narrative describing why you’d want to use a technique. This long homage to the command line is my attempt to remedy that situation.

Note: I originally learned the shell using the newfangled csh (which was a huge improvement over the original sh.) When I first started using Mac OS X, I tweaked it to use tcsh because that’s what I knew and loved. Over time, I gave up using these tweaks and started using the default shell: bash. The following examples assume that you’re doing the same.

Editing Keys

My most recent discovery, and the one that made me realize a post like this would be helpful to a lot of fellow developers, was the revelation that an option-click in the Terminal window emulates arrow keys.

Say you’re entering the following in the command line:

Oops! You left out “awesome” and now find yourself tapping the left arrow a bunch of times before entering the missing word. The pain increases linearly with the length of the command line you screwed up. If your Mac keyboard is anything like mine, the arrow keys are shiny from constant wear and tear.

Luckily, you can give those keys a bit of a rest with this simple trick: try Option-clicking on the first letter in “test”. The cursor is just where you want it and you haven’t touched the arrow keys!

To get a better idea about how this works, try this:

Now, press the arrow keys. The terminal emulator sends an escape (^[) followed by an open bracket and then A for up, B for down, C for right, and D for left. Now hold down the Option key and click with the mouse: the Terminal app just emits arrow keys to move the cursor block between the source and destination locations. This means it also works in tools like vi or even the Xcode debugging panel: a huge time saver just got even better!

(Use Control-C to get out of this echo mode and back to your shell prompt.)

While we're on the Option key, you can also hold it down while using the left and right arrows to move the command line cursor by a full word instead of a character. Which is exactly what you need when your editing a path with a missing directory name.

The command line also responds to control keys. The ones I use the most are Control-A and Control-E to move to the beginning and end of the line. Control-U and Control-K are also useful to delete text from the cursor to the beginning and end of the line buffer. I've heard that these are standard emacs key bindings, but can't confirm this since I'm a vi LOVER NOT A LOSER

Note that the Control and Option keys also work in standard Cocoa controls. In a Finder window, you can use Command-Shift-G to change the folder path and then use same muscle memory that you've acquired in the shell.

For those really long commands, you'll probably want to get into a more comfortable editing environment. Just use Control-X followed by Control-E to open the command buffer in your EDITOR. (More about setting up the EDITOR in just a bit.)

Another great key to know about is Tab. Try entering this:

Press Tab once and it completes "/Vol" to "/Volumes". Press Tab twice and you'll see a list of all mounted volumes. Welcome to the Lazy Typist Club™.

Shell Setup

As I mentioned earlier, I no longer configure which shell I use on a new OS X install. I do however, change the bash configuration on every Mac I touch.

Every time a new Terminal window is opened a shell process is created with your current login. As the shell is initialized, the .profile file in your home folder is used to initialize the interactive shell. Basically, you can think of .profile as a bunch of typing you don't have to do each time a new Terminal window is created.

(The name .profile dates back to the original sh. See "man bash" for a ton of more info and options.)

Here's my .profile. I like to keep it fairly simple:

Let's take a look at each group of settings.

Aliases

Aliases let you define command shortcuts. Since I'm old and forgetful, there aren't many. I used to have a lot of aliases, but found myself constantly using the alias command to list them out and find the right one. Which, of course, defeats the entire purpose of a shortcut.

The alias ll lets me list files in a format that is more readable, especially with the large files. I like using con instead of firing up the Console app (which is total overkill for most situations.) It should be pretty obvious how to create your own aliases. A lot of the command tricks you'll learn below will be good candidates if you use them often enough.

Search Setup

By default, the shell allows you to use Control-R to search previous commands. After typing the control sequence, your command history is searched for each letter that you type. Cool idea, but in my opinion, it's a terrible user experience. The reason is that my history is filled with entries that are very similar. If you have both "ssh CHOCKMASTER@domain1“ and "ssh CHOCKMASTER@domain2“, there's just too much typing to get the right match.

The next two lines in my .profile solve this problem: the bind command tells the shell to do a history search when the up and down arrow keys are used. When the shell is in this mode, you can just type "ssh" and then use the arrows to select the command you want to run again. This fits my needs much better and feels more consistent with the shell's default ability to move through the history with the up and down arrow keys if you haven't entered any text into the edit buffer.

Environment Variables

Finally, there are the environment variables. Again, i've whittled it down to the bare essentials. The EDITOR variable is used by Control-X, Control-E in the shell and lots of other tools. You can change it to emacs, but then I'd laugh at you.

The CLICOLOR variable is used by the ls command to show files and folders with color coding. You can change the colors using the LSCOLORS environment variable, but the configuration string is just too damn arcane for me, so I skip it and go with the defaults. See the man page for ls to learn more about the color coding and the options.

Finally, there's the PATH environment variable. The items, separated by a colon, are directories in the file system that contain the commands I use. By default, these paths are "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin".

I have a bunch of command line tools and scripts that I've accumulated over the years and they are all in the bin directory of my user folder. I also use MacPorts, so the /opt binaries get added to the end of my path.

Note: I've added a backslash to the end of the first line that contains the PATH definition. This is the "line continuation character" and can be used to break up lines that are too long for your editor, terminal or the narrow column of this web page :-)

If you use Xcode, you might find it handy to put the /Tools folder in your PATH. If you want command line access to the same versions of gcc, git, svn and other tools that Xcode uses, you can substitute /Tools with /usr/bin. Since I prefer to use MacPorts to get specific versions of the tools I use, I generally don't need access to Xcode's binaries. If I do, it's easy enough to do this:

Note that since the .profile is only read when the shell starts, the XCODE environment variable won't be correct if you run xcode-select after the shell is started. This is usually only a problem if you're juggling multiple versions of Xcode and can easily be solved by closing the Terminal window and opening another.

Shell Scripts

As I said before, I populate my ~/bin directory with a bunch of useful tools that I've developed over the years.

Two of my favorites scripts in that repository are psc and opensim.

psc

The first script is an oldie but a goodie (note the shebang — tcsh!). I named it psc:

It's a wrapper for ps that shows all of the process' command and can target it for a specific app. For example:

Now you'll see all of the processes that have "Xcode" in the command. The process ids are also shown, so you can kill them, of course.

I also find that seeing the parent process id, which is shown in the second column, is important in these days where everything seems to be a child of launchd. It's also helpful for finding things like XPC processes and other items that get executed from an app's bundle.

opensim

If you're an iOS developer, the opensim script is very helpful for finding your app's sandbox folder in the Simulator. Here's the script:

And here's how to use it.

Version Control

Most of the other stuff in my ~/bin directory are utilities that help with my own workflow (managing servers, testing, backups, etc.)

One of the reasons I prefer to keep these kinds of tools in separate files (as opposed to aliases) is that they're easier to manage from a version control repository. When I set up a new machine, I just go to the User's home folder and checkout my bin directory. Fin.

Another reason that I use files instead of aliases is that they're easier to search. For example, if I'm looking for a script that I wrote three years ago and I know it uses ssh, I just do this to jog my memory:

Shell Tricks

Once you get your shell setup, it's time to learn some of its most useful tricks.

History

The shell remembers everything you type. With a few simple characters, you can avoid retyping. The first two are !! which repeats the last command entered. I'm ashamed to admit that I do this every time I edit my /etc/hosts file:

"Oh crap, it's read only."

If you just want to reuse the last item in the command, which is typically a file name, then you can use !$:

Merlin has Inbox Zero. I HAVE TODO LIST ZERO

The history command will give you a list of the last 500 things you've typed:

That's a lot of stuff, huh?

If you want to execute one of those commands again, just use an exclamation point followed by the sequence number. For example:

Because the history list is so long, there are a couple of strategies for dealing with its size. The first is to do a simple search:

Alternatively, you can use use less to search the list interactively:

There are a few other ways to reuse your shell history: do a search for "Event Designators" in the bash manual page.

Command Substitution

Another powerful feature of the shell is command substitution. When you place back ticks around a command, the results are used in the current command. For example:

Not only does this save typing, but it makes sure that you're in the right place if you have multiple versions of Xcode installed. Bring on the beta releases!

Note: This is essentially how I setup the $XCODE environment variable in my .profile earlier.

Command substitution isn't limited to just paths, either. Try this:

Desktop Integration

As software developers, we live in two worlds: the command line and the desktop. Not surprisingly, Apple has provided several tools that helps you unite these two worlds. The following are some of my favorites.

open

The first tool is called open. Both the name and a manual page that says "opens files and directories" belie its true power.

For example, say you want to open the shell's current directory in a Finder folder. It's this easy:

If you want to reveal the enclosing folder for a directory or file, then just add the -R parameter:

The default is to open a file item in the Finder, but you can also specify and application using the -a parameter. Here are a few examples:

Since this is a Unix tool, you can open the standard input in an application using the -f parameter:

The open command assumes that text will be used for standard input, so if you want to use it with other file types, you'll need to use a temporary file. For example:

And since URLs are just another kind of file in OS X, you can use open to download them:

Or open your favorite network share points:

You can also include a "username:password@" before the host name, but I recommend that you just let the Finder prompt you for the login information. Remember that your shell history contains the last 500 things you've typed, so protect yourself from some smart-ass like me that sits down in front of your Terminal and does this:

Drag & Drop

As you can see, there are many different ways to go from the command line to the desktop. Conversely, going from the desktop to the command line is remarkably simple: you can drag any file or folder from a Finder window into the Terminal window and you get it's path.

Boom.

Clipboard

As developers, we live and die by our clipboard. Code and data moves between different contexts all day long thanks to Cocoa's NSPasteboard. It should not be surprising that pbcopy and pbpaste are simple and powerful integration points at the command line.

Want to see the contents of your clipboard or put it in a file? Here you go:

Going the other direction, you can copy the current month's calendar to the clipboard and then paste it into another app:

Simple!

defaults

Most apps have preferences that are managed by NSUserDefaults. You can easily view or modify these settings from the command line using the defaults command.

Let's start by looking at all the preferences for Xcode:

Looks more like a database than preferences, right? Every setting is shown with its key and associated value.

If you're interested in a single key, you can use it to limit the output. Say you want to get the information of every iOS device you've used in the Xcode Device Manager:

You can also specify a new value after the key. Set the one true tab width using:

Sometimes settings are used to persist data across launches. There are often cases where you want to get rid of things without wiping out the entire database. Here's an example of removing the recent text completions in Xcode:

Note that using the defaults command is much safer than editing a .plist file in ~/Library/Preferences by hand. Beginning in Mavericks, there is a cfprefsd daemon that manages and caches updates to these files. If you use a text editor to modify the file directly, both you and your app will get confused when the changes don't propagate through the cache managed by the daemon.

say

You know your Mac has pretty great speech synthesis built-in. But did you know is available from the command line as well? Say hello to:

Do you ever find yourself with a process that takes a really long time to run? Something like realigning the cores on Marco's new Mac Pro:

When the script finishes running after a few hours, you'll hear "cores realigned". Even if you’re not looking at the Terminal window, you’ll immediately know why everything feels so much snappier.

Now imagine the fun you can have when you ssh into a designer’s Mac with an open Skype microphone nearby:

Hugs and kisses, Louie.

screencapture

Speaking of designers, one of the best ways to communicate with them is through pictures. The screencapture tool let's you do some things you can't do using the Command-Shift-3 and Command-Shift-4 keys in the Finder.

If you need to take a screenshot of the entire screen and want to put it in a new email message, just do this:

Sometimes you need to get things setup before taking the screenshot (opening menus, for example.) So just tell screencapture to wait ten seconds:

The -P option tells the tool to open the captured image in the Preview app, too. That's often helpful to make sure you got everything you wanted in the shot.

If you're going to paste the image into an image editor, use the -c option to put the shot on the clipboard:

If you're interested in getting just a portion of the screen, use the -s to select a portion using the mouse. You can also specify different output formats with -t option:

As you've seen, this tool has a lot of options, so I usually refresh my memory with the built-in help:

mdls and mdfind

Spotlight search on the Desktop has become an essential tool for developers. We find code, documentation, messages and all kinds of information that's related to our projects using Command-space and a simple text field. Would it surprise you to know that you can do more complex searches of the same dataset using the command line?

To give you and idea of how much searchable data is available for Spotlight, use the following command on one of your Mac's hard drives:

The Spotlight index on my MacBook Air's 256 GB drive is a whopping 1.6 GB.

So how do we dig around in this huge database? The first step is to get an idea of what the key/value pairs in the index look like. The mdls tool is the way to do this:

Try this command on a few document files as well (folders and files have different metadata.) The keys usually start with "kMD”. For example, an item’s name in the filesystem is stored with the kMDItemFSName key. The values are everything after the equal sign.

Now that you've got an idea of what keys and values can be used, let's do some searching!

At its simplest, mdfind is a command line version of the Spotlight search in the upper-right corner of your menubar:

FYI THATS AN INTERPRETED DANCE DUH

This facility starts to get more powerful when you start using keys and values in the query. For example, this is a simple query that lists all items, both files and folders, that have a custom icon:

(Remember this command when we start digging around in resource forks below.)

These queries can be combined with || or && to form more expressive searches. Here's how you'd find all Photoshop files that have a layer named "Layer 1":

I found out that PSD files have a kMDItemLayerNames key by using mdls on a sample file. This is generally faster than Reading The Fine Manual.

The search through the metadata can also be limited to items that are in a specific folder. For example, here's how you'd find all the items in a Project folder that have been localized in Japanese:

Another great feature of mdfind is that you can pipe it's output to xargs to run other shell commands on the results. For example, the following command will find all apps that are PowerPC-only and send them to xargs (note the usage of -0 in both commands to delimit the listed items.) In turn, xargs will use du to show how much space each app uses and a grand total at the end:

If you're like me and have been migrating your Applications folder for years, there are a surprisingly large number of items that are wasting space. Doing the actual work of removing these items is left an exercise for the reader.

(Hint: rm is a shell command just like du. Be careful and backup first.)

Application Integration

It's incredibly handy to control your desktop apps using the shell. Since AppleScript has always been the best way to control apps, it makes sense that there would be a command line tool. The osascript tool is one the Swiss Army would love.

Want to change the volume of your speakers with a shell script? Go for it:

You can also tell the Finder to do common chores:

It's the end of a long work day and you have a script that needs to run for a few hours. It would be really nice to sleep your Mac after that job is finished, wouldn't it?

It's kind of a shame that Marco won't be there to see how snappy the realigned cores are, but hey, whatever.

AppleScript from your shell can also relieve frustration:

Note that this is a more friendly way to do it than killall Messages since the "quit" Apple Event gives the app a chance to shutdown gracefully. Not that Messages would really notice.

If you want to switch from the Terminal to another application, use this:

You can also use AppleScript to get information about apps:

Or get the properties of any file or folder:

Note the use of ¬ as AppleScript's line continuation character. You can get this character into your editor by using Option-L on your keyboard.

File Tools

We're developers. We love files. In more ways than ls, cat, and rm can ever know.

Let's meet some new friends...

file

How often have you opened a Get Info window in the Finder just to know the dimensions of an image or other basic information about a file in a project? The Finder is fine, but you're already at the command line, so just use file instead:

Getting the file information from the command line is often faster if you're dealing with a lot of different resources. For example, when your designer gives you a bunch of PNG files and you need to set sizes for them in code, here's how you get all the non-2x dimensions:

Much quicker! And file can tell you things that the Finder can't. Like the architectures supported in a binary:

This is particularly helpful when you're getting link errors for a .dylib library that's in your project: file will probably tell you that an architecture you need is missing.

You can throw pretty much anything at file:

Note that it doesn't know that .entitlements is an XML plist. But we've still got less to figure that out.

It's amazing how many different types of files can be examined using this simple command. To get an idea:

If you ever run across a Newton package or some Applesoft BASIC, file has you covered.

Quick Look

As its name implies, Quick Look is an extremely fast way to check the contents of a project asset. Want to make sure your iOS app's launch images are correct without leaving the command line?

The arrow keys let you scroll through the images and a tap on the space bar gets you back at a command prompt. This is a much quicker and easier way than viewing the images with Preview or an open command—your hands never need to leave the keyboard!

To make this even easier, I have a short shell script named ql:

I use it every day to check one or more files:

Extended Attributes

While Spotlight’s index is an external source of metadata about files, the OS X file systems also support extended attributes. Like Spotlight’s metadata, these can be thought of as key/value pairs that are attached to any regular file or directory.

To view these attributes, you use the xattr utility:

Another way to get the same information is to use the -l@ option with ls.

The tweets.zip file that was downloaded has three extended attributes. But what’s in these attributes?

The simple answer is that you don’t know: the use of reverse domain names is the first hint that the data is application-specific. Apple clearly uses these attributes for their own internal needs. Additionally, any application can attach its own information to a file using this mechanism.

In practice though, you can usually figure out what’s being stored there using the -p and -lp options. For example, after you look at the contents of the attribute, it’s a pretty safe bet that com.apple.quarantine is used to mark files that have just been downloaded:

In fact, if you want to skip the warnings from the Finder about the dangers of opening files, you can remove the quarantine attribute:

Since any data can be attached to the named attribute, sometimes you’ll see a bunch of hex values instead of text:

If that happens, just use the -lp option instead:

That “bplist” looks like a hint to the contents of the data. More about that in just a bit.

Extended attributes are pervasive: you’ll find that they’re working quietly behind the scenes to enable a lot of functionality we take for granted. For example, have you ever wondered why Xcode build folders never get backed up by Time Machine? Here’s your answer:

Resource Forks

If you’re a long-time Mac developer, you’ll remember the days of resource forks. They were kind of like the extended attributes shown above, but the attribute names were limited to four characters and the data being stored was only accessible on a Mac.

Some things never die: Mac files can still have a resource fork. These days, they’re mostly used to store custom icons that have been attached to a file or folder.

To access the resource fork of a file, just append “..namedfork/rsrc” to the path. For example:

The file has zero bytes of data, but the resource fork uses 155 KB. Let’s throw it at file and see what happens:

Close. It’s actually a Mac ICNS resource. I'm pretty sure this is the first and only time I've seen file get it wrong.

If you’ve ever noticed a zero length file named “Icon?” in a folder that has a custom icon, it’s the same thing:

Note: to get the ^M in the name, type Control-V followed by Control-M. You can do this to get any control character into the shell’s edit buffer.

Internally, the resource forks are stored as extended attributes using com.apple.ResourceFork. If you try to access it with xattr you’ll get an “operation not permitted” error.

Finder Info

Another extended attribute that can contain useful information is com.apple.FinderInfo. This chunk of data contains, well, information for the Finder:

Unless you're me, you don't recognize the extreme importance of 0x43, 0x48, 0x4F, and 0x4B. Luckily, there are some helpful tools in your Xcode Developer Tools folder. The first is GetFileInfo:

The man page for GetFileInfo explains the attributes. The creator, and source of those four hex bytes mentioned above, needs no explanation.

To change the file info, you'd use SetFile. That being said, it's probably easier to do it in the Finder's Get Info panel:

Fun Fact: These tools have been around since the days of MPW. If you don't know what MPW means, consider yourself lucky. In fact, pretty much everything in $XCODE/Tools is a trip back in time. Do this if you remember them as the "good ol' days":

Examining Binaries

A lot of the files we deal with are executable. Even if symbols have been stripped from the app, you can still infer a lot of information by looking at the null terminated strings present in the data:

Better yet, take it a step further and look at the information the Objective-C runtime uses in the Mach-O binary:

I think it's safe to say that the iOS Jailbreak community would not formed as quickly as it did without this tool. It's that important for figuring out what another developer did in their Objective-C code.

If you don't have class-dump installed on your development machine, fix that right now. And while you're there, don't be a tightwad: put some money in Steve Nygard's tip jar. He's been supporting and maintaining this free tool for years.

Test and Debug

So far we've pretty much treated the shell as a static environment. But we all know there are lots of gears and levers moving behind the scenes. The shell is a great place to watch that activity.

top

When I'm in a shell and want to see the overall activity on a system, this is the first tool that I turn to:

The -d option shows deltas for network and paging activity (which is much more useful than total counts.) I typically run this command when some process is running amok: -u sorts the list by CPU usage. Using -s 10 updates the output every 10 seconds, minimizing the sampling affecting in the results. A longer sampling period also helps keep the listed processes from jumping around too much.

On the desktop, I always run the first Cocoa app I ever wrote: iPulse presents this information (and more!) in an interface that's easy to read at a glance.

lsof

Have you ever tried to eject a disk and had the Finder tell you it can't because there are still files still open? You've probably seen lsof used as a way to tell you what those files are:

But as with most great Unix tools, lsof is not a one-trick pony.

For example, you can use lsof to see all the files an app has open. Let's look at everything the Finder has open using the -c option:

Yeah, that's a lot of files. So use grep to narrow down the search:

Hmmm… the large ArtFile.bin file in CoreUI.framework sure looks interesting.

And remember that Unix sockets are just another kind of file. So in addition to checking what normal files a process has open:

You can also check what kind of network connections it has open and the state of those connections:

Taking the network theme a bit further, the -i parameter lets you see which processes have open connections on specific ports:

Want to see all the processes that are listening on a socket? Here you go:

Or ones that have established connections:

Remember that sockets are often used for interprocess communication, so just because there's a connection, it doesn't mean that packets are leaving your Mac.

In general, lsof is a great tool for investigate processes that are unknown, poorly documented, or you're just curious about:

You probably didn't realize it at first, but you just got a list of all applications that are using the Keychain. Note that sudo is required because the securityd process runs with elevated privileges.

As you can see, there are a lot of options and parameters, so make sure to take a look at the lsof man page.

DTrace

If you're developing for Mac or iOS, you already know how damn useful Instruments is for tracking application behavior. DTrace is the framework that makes all that possible. Well, take a look at all the stuff in the shell that "uses DTrace":

Whoa. That's a lot of stuff. A shorter and more useful list can be had by just showing the "snoop" tools:

As you saw above, lsof gives you a snapshot of the current state of processes and the files they have open. The "snoop" tools, on the other hand, show you the system as it changes state. For example, let's use opensnoop to watch the system logging process as it opens files:

Every command that uses DTrace requires elevated permissions to retrieve the buffered data from the kernel. Get used to typing sudo.

Now, in another Terminal window, send something to the system log:

The window running open snoop should show something like this:

Have you ever wondered how crackers find the super secret file you use to store serial numbers? Use the -n to specify a process by name:

There you go. Good luck trying to outsmart the kernel with your "uncrackable" protection scheme.

If you're interested in seeing which processes open a specific file, use -f to specify a path:

You can also track file or disk activity with rwsnoop and iosnoop:

As you saw with that long list of DTrace shell commands, there's a lot more you can do with this facility. In fact, you can actually write code that gets compiled and run. Here's a one-liner that shows when processes are spawned and exec'ed:

In fact, those "snoop" tools you just ran are just script wrappers around code written in the D language:

For more information and tips about using DTrace on Mac OS X, check out the DTrace site.

fs_usage

Another tool to watch what's going on in the filesystem is fs_usage. This tool reports any activity for calls to like fopen(), fwrite(), getattrlist(), fsync(), etc.

For example, if you want to watch the Finder manipulate files:

When using this tool, remember that a "file system" doesn't necessarily mean a disk is involved. The -f option lets you filter out network related events:

tccutil

If there was an award for the crappiest OS X man page, I'd have to award it to tccutil:

Only one service is listed in the man page: "AddressBook". There are others, and now that you know how to poke around in binary files, you can find them:

If you're testing an app that requests for a user's calendar information, you can reset the authorization by removing the "kTCCService" prefix from the list above:

Be careful when you're using this tool: you're resetting all apps in each service category, not just your own. I do not recommend running this command on a machine where you have apps already registered in System Preferences > Security & Privacy > Privacy. Use a test machine or a clean VM image before you start blasting away at security settings.

It's also worth noting that Accessibility is a special case. There is a SQLite database that contains the bundle ids for the applications that have been granted access. And you can modify that database:

Note that these commands have no effect on whether you see the authorization prompts the first time a user accesses the service from your app. After a new app been presented with the alert dialog, an entry is added to the com.apple.universalaccessAuthWarning defaults:

You can hunt for specific paths and bundle IDs, or just use the nuclear option:

After clearing the warnings, you'll need to restart your Mac and run the app again. Again, this is much more palatable if you're working in a test environment on a dedicated machine or VM.

When your working with the above commands, remember that as far as security is concerned, you'll be getting a bundle ID for Xcode when running under the debugger. In many cases, you want to get rid of settings for com.apple.dt.Xcode as well as ones for your own app.

The Internet

True story: the Internet existed on the command line before the Tim Berners-Lee ever thought about making a browser in the early '90's. First there was Telnet in 1969, then FTP in 1971, followed by Finger in 1977, and so on.

I think it's pretty safe to say the command line will never die, but the best part is that your Terminal continues to be a tool that works great in our networked world.

curl

Every time I upload a new version of our software, I check it using curl:

This lets me see that a "200 OK" response is returned and that the number of bytes match my local copy.

Another great use for curl is to watch all data that gets transferred over a HTTP connection. And I mean ALL the data:

The title of curl's man page is "transfer a URL." That all encompassing description should be your first hint that the tool does a lot. And I mean a lot—it supports the DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP protocols.

If you use URLs in your work, do yourself a favor and check out the man page.

tcpdump

Did you know that you can write code that runs in the OS X kernel and looks at every network packet at the data link layer? And that you can do it from the command line?

The tcpdump command is a user interface for Berkeley Packet Filters. This powerful tool can show you anything that's happening on your network.

A simple example is to watch what's happening when you access a web resource. Say you want to see all the HTTP data that passes on the network when you load Gruber's latest post in your browser:

The -s parameter sets the amount of data you see in each packet: when it's set to zero, you get everything. The -A parameter specifies that the data should be displayed in ASCII. You need to use sudo with tcpdump because you're accessing the /dev/bpf* devices which have root-only permissions.

In conjunction with Internet Sharing, tcpdump can be a very powerful tool for iOS developers. After setting up Internet Sharing in your Mac's System Preferences, you can then watch traffic that passes between the Ethernet and Wi-Fi interfaces of that shared connection. If your iPhone is connected over Wi-Fi to your Mac using the IP address 192.168.2.2, the following command will show all the requests it sends to Daring Fireball:

If you were interested in packets going the other way, just swap the src and dst. Hopefully, you'll see how this is useful when debugging network traffic between your iOS device and a REST service.

The syntax used to write the packet filter code is defined in the pcap-filter manual page ("pcap" is short for packet capture.) The filtering options in the expression include detecting different network protocols and examining the length and contents of the packets themselves.

Web

Have you ever had a folder full of files that you've wanted to access through a web browser? You could setup Apache to do this by editing the httpd.conf file, or just enter the following command in the folder you want to access:

Now, when you open http://localhost:8000 in browser, you'll be able to traverse the directory structure. Note that the server runs until you use Control-C to get back to the shell prompt.

This can be a great tool when testing your code: the directory can contain static JSON files that include data for your app. Just update the endpoints in your code to use the localhost and you're set.

DNS

Caches are designed to store information that's used repeatedly. But when that data gets stale, you need a way to flush the cache. As developers, we're often faced with DNS or other changes that affect our network stack.

When you find some piece of software misbehaving on the network, it's often caused by the Directory Service cache being out-of-date. Luckily, there's a simple command to flush the cache and get things back in working order:

If you're having problems resolving IP addresses from domain names, you can restart the DNS server with:

A similar command causes statistics and other diagnostic information to be logged to /var/log/system.log:

Be prepared to sift through a lot of information!

Services

Every Unix system has a simple tab-delimited database of service names and ports. The file is located in /etc/services. If you're wondering what's listening on port 69, do this:

You'll also see that the TFTP service can use both TCP and UDP protocols.

Another handy search is if your looking for the standard ports for a protocol. Say you want to know which ports can be used for IMAP:

IP address

Need to know the WAN IP address you've been assigned outside your internal LAN?

Loading the site in your browser shows a lot of other options. Be aware that information about the HTTP connection and HTML content aren't available from the command line unless you explicitly set them as options for curl.

Conversions

Data is never in the format you need it, is it? The shell's notion of standard input and output has always made it great for doing data conversion. Here are some tools that you may not know about...

Format

Want to convert text between .txt, .html, .rtf, .rtfd, .doc, .docx, .wordml, .odt and .webarchive formats? Look here:

It's pretty hard to read a binary property list, so convert it to XML or JSON. Or vice versa:

Your designer gives you individual PNG files for your app icon, but your project needs an .icns file. Or vice versa:

Pretend like you're in The Matrix:

Or convert between hex and binary formats:

Compression

How many ways do we need to compress data? In the world of the command line, I'm pretty sure the answer to that question is a number that approaches infinity. Regardless, here are a few of the most common formats and the tools used to view and extract the data from the command line.

If you have a ZIP file and you want to quickly view its contents, use the unzip command with the -l option:

Without the option, the ZIP file will be extracted to the current directory:

Similarly, a GZIP compressed TAR archive can be listed with:

And extracted with:

Fun fact: the name tar is short for "tape archive". That gives you an idea of how long this piece of code, and resulting archives, have been around. I think it's pretty cool that an archive you created in the 1970's can still be used forty years later. How much code being written today will be able to say that?

Log files are often compressed with either GZIP or BZIP. A quick way to uncompress these files and view their contents is to use the "cat" variants and pipe the output to less:

Disk Utilities

If you're looking for a disk utility, look no further than diskutil. This is essentially the command line version of the Disk Utility in your Applications > Utilities folder.

The first command I use is list:

This gives you the layout of all attached disks. The identifier column is typically used as a parameter for the other commands. For example, if you want to see all the information about your Apple_HFS partition, you'd use the identifier for that partition:

A similar command gets information about the disk itself. In this case, we're also using grep to quickly get the SMART status for the drive:

Some of the commands can use volume names instead of identifiers. For example, to eject a volume, you can use:

Be careful with diskutil since it also provides commands like secureErase and partitionDisk—they can quickly and permanently destroy your data. Expert users also use this command to setup and manage software RAID sets.

Another simple disk utility is the df (the short form for "disk free".) To show how much free space you have on your boot volume, use:

Unicode

Did you know you can use Unicode in shell commands? Yep:

$ echo $'\xf0\x9f\x8d\x94'

Show more