Handy terminal commands on a Mac

Joel Malone
3 min readSep 2, 2021

--

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:

history

Which will output your recent commands:

992 yarn serve
993 cd somewhere
994 node do-a-thing.js
995 cp ~/Downloads/file.text elsewhere
996 zip -9 compressed.zip *.txt
997 curl http://localhost:12345/status
998 ssh hello@my-dev-machine
999 scp hello@my-dev-machine:/opt/downloads/something.zip .
1000 open .

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

history | grep “tail”

Which outputs something like this:

592 tail -f -n +0 log.txt | grep -i ‘error’

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:

tail -n +0 -f log.txt | grep -i ‘error’

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:

find /usr/local -path “*sqlite3”

Which outputs this:

/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Aliases/sqlite3
/usr/local/opt/sqlite3
/usr/local/Cellar/sqlite/3.36.0/bin/sqlite3
/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sqlite3

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

find /usr/local -path “*sqlite3” | grep -e “sqlite3$” | grep “/bin”

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:

/usr/local/Cellar/sqlite/3.36.0/bin/sqlite3

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:

scp hello@my-dev-machine:/opt/downloads/something.zip .

…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:

curl google.com

Which will show you the HTTP response body:

<HTML><HEAD><meta http-equiv=”content-type” content=”text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF=”http://www.google.com/">here</A>.
</BODY></HTML>

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

curl -v google.com

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

* Trying 142.250.66.206…
* TCP_NODELAY set
* Connected to google.com (142.250.66.206) port 80 (#0)
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 02 Sep 2021 08:48:58 GMT
< Expires: Sat, 02 Oct 2021 08:48:58 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
<
<HTML><HEAD><meta http-equiv=”content-type” content=”text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF=”http://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact
* Closing connection 0

--

--

Joel Malone

Software Engineer living in Southwest Western Australia