2

dnf list installed output is human readable but not very friendly to machine parser. is there a command allow us get the installed package in json or csv or other parser friendly format? Does dnf have python binding provide nicer interface?

1
  • Do you actually need the dnf-specific information (such as which repository a package came from)? If not, you can cut out the middle man and just ask rpm for the list of installed packages (rpm -qa for example)
    – TooTea
    Commented Sep 24, 2021 at 10:26

2 Answers 2

7

The rpm command provides some more flexible options for printing the result. For listing installed packages you can use rpm --query --all and format the output using --queryformat.

For example to print just names of the packages you can use rpm -qa --queryformat "%{NAME}\n". You can further specify the output in a printf-like syntax and you can use rpm --querytags to print all available "variables" rpm can print about the packages. Check rpm man page for more information.

Yes, DNF also has Python bindings, documentation is available here and to get installed packages you can do something like:

import dnf

base = dnf.Base()
base.fill_sack() 
q = base.sack.query() 
list(q.installed())
3

Is there a command allow us get the installed package in JSON or CSV or other parser friendly format?

It might also be possible by using post-processing in example like

dnf list installed | tail -n +2 | awk -F '[.]' '{ print $1 }'

providing a list of names of all installed packages only.

It could be converted into JSON format by adding

| jq --raw-input . | jq --slurp .

Thanks To

Other Q&A

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .