How to find files that are not owned by any package?

In /var/lib/dpkg/info are .list text files that list all the files contained in each package¹ installed through Debian's package manager.

Finding all files in the filesystem not matching any entry there can be achieved with something naïve like this:

find / -xdev -type f \( -exec grep -xq "{}" /var/lib/dpkg/info/*.list \; -or -print \)

This will obviously take a very long time as the whole filesystem will be scanned. If you use different partitions for system directories (such as /usr or /var), specify them after the initial /.

Warning: That does not include files created by package scripts. For instance:

  • /etc/hosts.allow is not listed anywhere but it might come from libwrap0 that possibly created it, if that file didn't exist at time of the package installation.
  • Many files are compiled during installation, for example .pyc files (compiled Python libraries), .elc files (compiled Emacs Lisp librarires), etc.

A more efficient version of @Patrice's solution, using a shell with support for process substitution (bash, AT&T ksh, zsh):

(
  export LC_ALL=C
  comm -23 <(find / -xdev -type f | sort) \
           <(sort -u /var/lib/dpkg/info/*.list)
)

Like Patrice's solution, it assumes no file path contains newline characters.


Since you tagged your question with debian the obvious choice not mentioned yet is to use cruft-ng if you don't require any flexibility or cruft if you don't want to search through the whole system/locatedb.