2  Basics

Things I probably should’ve learned a long time ago, but didn’t (or did, and forgot). So, here they are. Because these notes are primarily for my own reference, and I use a Mac (see My setup for details), the solutions here are targeted for that OS.

2.1 How to kill a process on a particular port

I usually end up searching for this when I go to test for the existence of something (e.g. chromedriver) by running it at the command line, and then want to stop it.

When you know the port number

If you know the port number (e.g. with chromedriver, the port number is listed on start up), you can find the process ID (PID) by port number using lsof, and stop/kill it with kill -9:

sudo lsof -i :<PortNumber>

kill -9 <PID>

When you don’t know the port number

When I don’t know the port number for whatever reason (or don’t think to look for it), I like to use htop, “a cross-platform interactive process viewer”, which you open by running htop (assuming you have it installed1).

Once open, you can search for the process by name using F3, kill it when selected with F9, and then exit htop with F10.


You certainly don’t need htop for this. The built-in ps command (short for process status) also shows which processes are running.

If you run ps on its own, it will return some basic information for the current processes, including their PIDs. However, ps has tons of options for process selection and output control which you can learn about in the ps Man page.

For examples using ps and/or other utilities, check out How to get the PID of a process by giving the process name in Mac OS X on StackOverflow.

2.2 How to display $PATH in human-readable format

To display the current $PATH in Linux, you use echo.

# also works *without* quotes around $PATH
# used here for consistency with subsequent command
echo "$PATH"

To display it in human-friendly format (i.e. with line breaks instead of colons), run:

echo "${PATH//:/$'\n'}"

2.3 How to send a list of items in a directory to a text file

I’m sure this could be a much more generic recommendation and that there are other ways to accomplish the same thing, but I happened to want to make a text file with a list of all of the folders in a directory.

First, navigate to the directory for which you want the list of contents. (If it’s a deeply nested folder, I like to do this by copying the full path from the Context menu in Finder, and then cd and paste it into the terminal).

From there, you can use the ls command with a redirect (>) to save the contents to a file:

ls > ~/the_file.txt

2.4 How to optimize all the images in a directory

For image optimization, I like to use the OptiPNG CLI.

On a Mac with homebrew set up, you can install it by running:

brew install optipng

While you can use optipng to compress individual or multiple files2, I wanted to go ahead and optimize all the PNGs in a directory (since it’s lossless compression). To do so, you navigate to the directory containing the files you want to optimize, and run:

optipng *.png

If you’re curious about other tools for PNG compression, I recommend checking out pngquant vs pngcrush vs optipng vs pngnq by Nick Mills-Barrett (Mills-Barrett 2014).

If you want more compression with OptiPNG, you can use the -o argument to specify a higher optimization level (the default is 2, and it goes from 0 to 7) (Dhillon 2020).

  1. If you don’t have it installed, but would like to install it, I recommend using homebrew’s htop formula.↩︎

  2. See the OptiPNG section of Nexcess’ How to optimize JPEGs, PNGs, and GIFs from the CLI using jpegoptim, OptiPNG, & Gifsicle for how to do that.↩︎