I wanted to install vim package on FreeBSD 13.0, and did so without realizing that there were over 86 additional dependency packages installed. This was done with pkg install -y vim command.

Anyway, I wanted to clean up by removing vim and those dependencies after all this is a headless server. Removing vim package doesn’t remove the installed dependencies.

Fortunately, I was able to copy and paste the names of dependency packages and save it to a file. My first thought was to just go through each line in that file and run pkg delete -y PACKAGE. In the past I needed to do similar thing in a bash script or command to loop through the content of a file per line. Guess what? I no longer remember how to do that. Hence I’m writing this post to remind my future self in case I need to do this similar thing again.

There are many ways to loop through each line in a file. A simple one that works for me is:

while read line; do
    echo "$line"
done < file.txt

In my actual use case, I had it as:

# while read p; do pkg delete -y $p; done < list_of_uninstall_pkg.txt

If you’re interested, you can learn a few other ways to achieve this here.

Reference: