2

I tried using apt-file list --regexp ".*", but it's not working, probably because the returned content is too extensive.

OffIGoThen
  • 21
  • 4
  • ` apt list --installed > filename` didn't fit your needs ? – francois P Aug 23 '23 at 12:07
  • I'm just curious what would be the use case or the problem you are trying to solve with this? – Esa Jokinen Aug 23 '23 at 12:17
  • This is the task assigned to me by my mentor, and I'm not sure of the exact purpose behind it. – OffIGoThen Aug 23 '23 at 12:20
  • Is this a homework or an actual task required in a business environment? – Esa Jokinen Aug 23 '23 at 12:23
  • I believe it is an acuactual task as my mentor seems to be in need of me to solve it.. – OffIGoThen Aug 23 '23 at 12:27
  • What is exact requirement here: - Do you need list of all the packages installed on the server OR - Do you need list of all the files installed with packages installed on the server. ? – nightWolf Aug 23 '23 at 12:34
  • I want to download file list of all packages in the apt database to my local system. However, the results are too extensive, and I can't achieve it with a simple command. – OffIGoThen Aug 23 '23 at 12:39
  • I need all the files installed with packages in apt database. – OffIGoThen Aug 23 '23 at 12:41
  • But the packages may not be installed or downloaded locally because I need all the packages. – OffIGoThen Aug 23 '23 at 12:44
  • 1
    wouldn't : apt-file search / # be faster ? (ie, apt-file search pattern) : no regex required, and all files have at least one "/" in it, and should match? – Olivier Dulac Aug 28 '23 at 08:55
  • and depending on the task your mentor intends to do ... _"To list all local files not belonging to any installed package, you might want to take a look at the cruft or the cruft-ng package."_ ( https://debian-handbook.info/browse/stable/sect.apt-file.html ) – Olivier Dulac Aug 28 '23 at 08:56
  • "apt-file search /" is indeed faster. However, I encountered the same issue as using regexp. The command eventually terminates with: "terminated by signal 13". I suspect it might be because there are too many results returned. I don't need cruft for now, but I will explore its usage further. Thank you very much for your response. – OffIGoThen Aug 29 '23 at 10:30

2 Answers2

3

You can pull file list for all the packages using command apt-file list --regexp ".*" however this will take time in gathering the list of all files. The same is mentioned on the man page of the command apt-file:

       -x, --regexp
       Treat pattern as a (perl) regular expression. See perlreref(1) for
       details. Without this option, pattern is treated as a literal
       string to search for.

       Be advised that this option can be rather slow.  If performance is
       an issue, consider giving apt-file non-regex pattern matching too
       much and pipe the output to perl -ne '/<pattern-here>/'.  This
       enables apt-file to use more optimizations and leaves less work to
       the "slower" regex.

Another way to pull file list for all the installed packages is by using below command:

for package in $(apt list --installed| awk -F"/" '{print $1}'); do
  dpkg --listfiles "$package";
done

You can adjust the output as per your requirement.

In case you want to pull file list for all the packages from apt db, you should go with apt-file which will take time as there are usually thousands of packages(depending on repos configured) in apt db, so there will be millions of files to list. You can go with either command:

apt list | awk -F"/" '{print $1}' > package_list
apt-file list -f package_list

or

apt-file list --regexp ".*"
Esa Jokinen
  • 46,944
  • 3
  • 83
  • 129
nightWolf
  • 108
  • 6
  • Thank you for your response, but what should I do if the package is not installed locally? – OffIGoThen Aug 23 '23 at 12:52
  • Just updated the answer as per your requirement. Hope this is helpful . – nightWolf Aug 23 '23 at 13:02
  • I tried this command, and it's indeed very slow. I guess it might take a day to complete the execution. I'm not sure if there's a faster method, but your response has already met my basic needs. Thank you once again. – OffIGoThen Aug 23 '23 at 13:08
2

Another option of listing all files in all packages in the APT cache, as the suggestions from @nightWolf utilizes apt, which is not good in scripts...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

#!/bin/bash

for package in $( \
  apt-cache dump \
    | grep -e "^Package" \
    | awk '{print $2}' \
    | sort )
do
  apt-file list "$package";
done

Again, the task is slow however you try to do it. The APT cache database exists and has the tools for performing all the relevant searches. Dumping this information out as text does not seem useful.

Esa Jokinen
  • 46,944
  • 3
  • 83
  • 129