You need to ask whatever package manager could have been used - only the package manager stores meta-information about what packages has been installed, and which files are in the package. There's no way to get such information from a naked file.
You didn't say what flavour of distribution you use, but I recommend always using a single package manager (apt for me), and putting stuff that is compiled by the user somewhere else (for me, /usr/local
, managed with stow
, and I keep a list of packages). This way it's easy to find out to which package a particular file belongs.
If you use a dozen package managers or so (why?), you could write a script that queries each of them in turn.
Edit
I keep track of stuff I compiled from source by making a file for each package I compiled in /usr/local/packages/<packagename>
. I need this anyway because it contains information about how I compiled it (often you need to make adjustments, fix a few things that won't compile with a newer gcc etc.) in case I need to compile it again when I upgrade it. And since I have this file, I also add information about where I downloaded the source, what version it is, what it does, what the name of the source tarball is, etc. I keep track of the installed files by installing everything into /usr/local/stow/<packagename>-<version>
(which also sometimes needs adjustments to Makefiles, or ./configure --prefix
, etc.), and then use stow
to make links to it in /usr/local
. I've described this in detail here.
You can also make really deb or rpm packages out of stuff you compile yourself, but this is more work than keeping track of it with stow
.
$ rpm -qa --last > last-packages.txt
– Knud Larsen Apr 19 '18 at 16:30$ ls -tl /var/lib/dpkg/info | grep list > list-deb.txt
– Knud Larsen Apr 19 '18 at 16:35