# How to clean up /usr/local



## Alain De Vos (Jun 16, 2021)

I want to find all files in /usr/local which do not belong to an installed package as given by "pkg info" and have them automaticly removed. Offcourse no removal in /usr/local/etc.
[ I have had several core dumps off pkg when installing (bad/wrong) .txz manually so there might be stuff lying around ]
Maybe if I list all the files and then lookup for "the package providing file" and in absence "rm -f" ?
I come up with

```
find /usr/home 2> /dev/null | xargs -I {} pkg which {} | grep "not found" | grep -v etc | awk '{print $1}' | xargs -I {} rm -f {}
```


----------



## Jose (Jun 16, 2021)

Sounds like a terrible idea. At least exclude /usr/local/etc from this "cleanup".


----------



## Alain De Vos (Jun 16, 2021)

This remembers me of my Windows time. Let's evaluate if bsd has better tooling for free.
There are 500.000 files in /usr/local, no kidding.
Decideded to taken the primelist and remove all packages, clean manually /usr/local. Seemed to be not much lying around. Reinstallation of the primelist is a bit slow. Looks like PC is slower in single user mode. I don't know why.
You must do installation in multi-user mode it is 10x faster
In comparison to Windows this is a much cleaner solution.


----------



## Alain De Vos (Jun 17, 2021)

Better idea,

```
pkg query -e "%a = 0" "%o" > primelist.txt
Remove all packages &
rm -fR /usr/local except etc
```
Compare to cleanup a Windows registry.


----------



## sidetone (Jun 17, 2021)

Removing packages does a lot of that. /usr/local/etc/ is left behind, but this directory isn't complicated.


----------



## Deleted member 30996 (Jun 17, 2021)

/usr/local/share is where all the fluxbox styles are located, Gimp tools, licenses and just about everything you do not want to delete with /usr/local.


----------



## mtu (Jun 17, 2021)

I'd `pkg list | grep ^/usr/local/ | sort > list1` and `find /usr/local/ | sort > list2` and then take a look at `diff list1 list2`.


----------



## covacat (Jun 17, 2021)

mtu said:


> I'd `pkg list | grep ^/usr/local/ | sort > list1` and `find /usr/local/ | sort > list2` and then take a look at `diff list1 list2`.


then comm -13 list1 list2 |grep -v /etc/|xargs rm


----------



## Jose (Jun 17, 2021)

Alain De Vos said:


> Better idea,
> 
> ```
> pkg query -e "%a = 0" "%o" > primelist.txt
> ...


For anyone reading this thread who might think this is a good idea, this will remove all configuration files for any services you have installed through ports or packages.


----------



## Alain De Vos (Jun 17, 2021)

True, some configuration is sometimes written sometimes outside /usr/local/etc.
Eg. sometimes in /usr/local/www
Which is a reminder that /var/www is a more logical place for it and /usr/local/www should be used for static pages.


----------



## Alain De Vos (Jun 17, 2021)

If you want to try and find your stale files in /usr/local , the following is doing that more or less,

```
find /usr/local 2> /dev/null | egrep -v -e "etc|ruby|perl|python|lib|www|share|include|man|GNUstep|drm-fbsd13-kmod|bootstrap-openjdk11" | xargs -I {} pkg which {} | grep "not found" |  awk '{print $1}'
```


----------



## PMc (Jun 17, 2021)

I do `pkg upgrade -f` along every quarterly. Finding stale/obsolete files is then just `find /usr/local -type f -ctime +1`.


----------

