Handy terminal commands on a Mac

Photo by Matthew Henry on Unsplash

Often, I learn new terminal commands, and then promptly forget them. I need some place that I can easily copy and paste them. So this is that.

The commands covered in this article are: open, history, tail, find, scp, and curl.

open

This one’s neat and simple: it opens Finder to the folder you give it.

Tada.

history

The most meta-useful command is the one you can use to remember commands.

Run it as-is to dump the recents commands you’ve used:

Which will output your recent commands:

Or, combine it with grep to find that one tail command that I use to monitor logs but can never remember:

Which outputs something like this:

You can then copy and paste the command to run it again.

tail

Speaking of tail, I find it useful to monitor file-based logs while coding, using something like this:

Which will:

  • Use -n +0 to output the entire contents of the file.
  • Use -f to follow the file, meaning tail will run forever, streaming new lines to terminal.
  • Grep the output to only show me the lines with “error” in them.

find

Use the find command to find files on your machine.

For example, I want to know where sqlite3 resides on my machine. I know the binary is called sqlite3, but where does it live?

Let’s use find:

Which outputs this:

The above is a bit messy, and we’re looking for a binary, so let’s use grep to whittle down the results:

In the above, we used grep to perform a regular expression “ends with sqlite3” match, and then grep again to ensure /bin occurs in the path.

Now the output is:

Thanks to this article by @carusot42 for these handy commands when working with sqlite!

scp

Use scp to download files from a remote machine using SSH.

For example, this command:

…will connect to remote machine my-dev-machine as user hello and download /opt/downloads/something.zip to the current local folder.

curl

Curl is great for quickly hitting an HTTP endpoint, like this:

Which will show you the HTTP response body:

Or add -v to see the behind-the-scenes stuff, too:

This produces way more information about the request and response, which is super handy for debugging HTTP endpoints:

Software Engineer living in Southwest Western Australia