5

Previously I've had things installed with homebrew which had dependencies which I omitted to remove when I removed the package itself (homebrew of course does not do this automatically for you, for good reason).

Now, to tidy up my system a bit, I'd like to identify all the brew packages which are not required by any other that is installed, so that I can manually identify those which I want to keep vs. those I am happy to remove.

To do this manually, I would do brew list, then, on each item which that outputs, I would do brew uses --installed <name-of-package-from-brew-list>, to check with respect to each package whether it is used by any other installed package (Then, if the answer is none, if I was curious as to why it was originally installed, I could also do brew uses <name-of-installed-package> which might indicate to me which package I used in the past but have since uninstalled actually installed it originally).

This is all very manual and I wondered if xargs could help.

My attempt to use it isn't working:

brew list | xargs brew uses --installed > test.txt

I get no output at all from that command, a blank file (but the command takes several seconds to run).

What am I not doing right with xargs?

mwal
  • 2,803
  • 26
  • 34

2 Answers2

15

It seems like brew leaves would fit your use-case?

% brew leaves --help
Usage: brew leaves

List installed formulae that are not dependencies of another installed formula.

From the question:

brew list | xargs brew uses --installed > test.txt

This command should be spelled xargs -n1 since brew uses with multiple formulae does something quite different:

% brew uses --help
Usage: brew uses [options] formula

Show formulae that specify formula as a dependency. When given multiple
formula arguments, show the intersection of formulae that use formula. By
default, uses shows all formulae that specify formula as a required or
recommended dependency for their stable builds.
jonchang
  • 1,434
  • 9
  • 13
  • nice, thanks. i saw discussion of `leaves` but didn't realise it was in homebrew itself. fyi re. the part of the question specifically about xargs, i simply added `-n1` to the xargs command above, but that still doesn't do as wished, some extra loop going on. – mwal Dec 20 '19 at 10:54
9
brew autoremove --dry-run 

Removes all packages that were only installed as a dependency of another formula and are now no longer needed. With the --dry-run flag you can dry run it and just get a list of all packages without uninstalling anything.

mklb
  • 1,215
  • 12
  • 8