# Remove stale & old files not belonging to an installed package in /usr/local



## Alain De Vos (Apr 8, 2020)

If do by example  "touch /usr/local/bin/delethisoldfile".
That file does not belong to an installed package.
How to get a list of all these files ?


----------



## unitrunker (Apr 8, 2020)

"pkg provides" plus xargs might work.


----------



## PMc (Apr 9, 2020)

This should be solveable from the pkg database.
Another way is, as I am doing full reinstalls (upgrade -f)anyway, afterwards something like `find /usr/local -type f -ctime +90` should show only some config files under etc (and share/emacs/site-lisp/default.el).
It will find such files only when they get old, but that is good enough for me.

There may, btw, also be stale stuff  in the base system - specifically if you do only upgrade-in-place, and happen to have changed src.conf options. So, a similar look into these places may not be wrong.


----------



## jb_fvwm2 (Apr 9, 2020)

#/usr/local/bin/#      pkg which ./* |grep not
#  -- some are false positives -- don't delete them -- are symlinks and will ruin parts of
          installed ports
#  -- some are false postives -- don't delete them -- are directories...


----------



## leebrown66 (Apr 9, 2020)

There are many files under /usr/local that are not installed.  Therefore I would not recommend just deleting everything this produces!

files-not-installed.sh

```
#!/bin/sh
# get a list of all files installed by all packages
pkg info --list-files -a | awk -f ./files-not-installed.awk | sort > /tmp/l1
# get a list of all files
find -x /usr/local -type f -or -type l 2>/dev/null | sort > /tmp/l2
# show difference
diff /tmp/l1 /tmp/l2 | less
```

files-not-installed.awk

```
$1 ~ /\/usr\/local\// {c=index($1,":");if(c==0){for(i=1;i<NF;i++){printf "%s ",$i}printf "%s\n",$i}}
```


----------



## SirDice (Apr 9, 2020)

You shouldn't get any 'left-overs', package build tests are quite strict when it comes to pkg-plist. There may be some configuration files left though, but certainly no binaries or libraries.


----------



## rim (Sep 25, 2020)

I use ideas from files-not-installed.sh
and write: http://www.netlab.linkpc.net/download/software/os_cfg/FBSD/12.0/base/root/bin/clean_ports.sh
Also I try to write same for system: http://www.netlab.linkpc.net/download/software/os_cfg/FBSD/12.0/base/root/bin/clean_system.sh


----------

