Right way to get the list of installed packages matching a pattern?












5















I'm on system running a (fairly recent-)Debian-based distribution.



I'd like to generate a plain list of all installed packages matching a certain pattern. I can do that by, running, say,



apt list --installed "linux-image-*" | cut -d/ -f1


but I get lines I don't care for, e.g.:



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

Listing...


So maybe I'd better not use apt. I can run dpkg-query like so:



dpkg-query --showformat='${Package}n' --show "linux-image*"


but that's not limited to installed packages. I could use



dpkg-query --list "linux-image-*" | grep "ii"


but then I'd need to do a bunch of text processing, and who can trust those spaces, right?



So, bottom line: What's the right way to get the list of installed packages matching a pattern?





Note:




  • Bonus points if it can be a proper regexp rather than just a shell glob.

  • Having to parse the text seems like a less-than-ideal solution; if that's what you suggest, please argue why there isn't a better way.










share|improve this question





























    5















    I'm on system running a (fairly recent-)Debian-based distribution.



    I'd like to generate a plain list of all installed packages matching a certain pattern. I can do that by, running, say,



    apt list --installed "linux-image-*" | cut -d/ -f1


    but I get lines I don't care for, e.g.:



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

    Listing...


    So maybe I'd better not use apt. I can run dpkg-query like so:



    dpkg-query --showformat='${Package}n' --show "linux-image*"


    but that's not limited to installed packages. I could use



    dpkg-query --list "linux-image-*" | grep "ii"


    but then I'd need to do a bunch of text processing, and who can trust those spaces, right?



    So, bottom line: What's the right way to get the list of installed packages matching a pattern?





    Note:




    • Bonus points if it can be a proper regexp rather than just a shell glob.

    • Having to parse the text seems like a less-than-ideal solution; if that's what you suggest, please argue why there isn't a better way.










    share|improve this question



























      5












      5








      5


      1






      I'm on system running a (fairly recent-)Debian-based distribution.



      I'd like to generate a plain list of all installed packages matching a certain pattern. I can do that by, running, say,



      apt list --installed "linux-image-*" | cut -d/ -f1


      but I get lines I don't care for, e.g.:



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

      Listing...


      So maybe I'd better not use apt. I can run dpkg-query like so:



      dpkg-query --showformat='${Package}n' --show "linux-image*"


      but that's not limited to installed packages. I could use



      dpkg-query --list "linux-image-*" | grep "ii"


      but then I'd need to do a bunch of text processing, and who can trust those spaces, right?



      So, bottom line: What's the right way to get the list of installed packages matching a pattern?





      Note:




      • Bonus points if it can be a proper regexp rather than just a shell glob.

      • Having to parse the text seems like a less-than-ideal solution; if that's what you suggest, please argue why there isn't a better way.










      share|improve this question
















      I'm on system running a (fairly recent-)Debian-based distribution.



      I'd like to generate a plain list of all installed packages matching a certain pattern. I can do that by, running, say,



      apt list --installed "linux-image-*" | cut -d/ -f1


      but I get lines I don't care for, e.g.:



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

      Listing...


      So maybe I'd better not use apt. I can run dpkg-query like so:



      dpkg-query --showformat='${Package}n' --show "linux-image*"


      but that's not limited to installed packages. I could use



      dpkg-query --list "linux-image-*" | grep "ii"


      but then I'd need to do a bunch of text processing, and who can trust those spaces, right?



      So, bottom line: What's the right way to get the list of installed packages matching a pattern?





      Note:




      • Bonus points if it can be a proper regexp rather than just a shell glob.

      • Having to parse the text seems like a less-than-ideal solution; if that's what you suggest, please argue why there isn't a better way.







      apt package-management dpkg






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 17 '17 at 21:40







      einpoklum

















      asked Dec 17 '17 at 21:27









      einpoklumeinpoklum

      2,17642153




      2,17642153






















          4 Answers
          4






          active

          oldest

          votes


















          7














          aptitude supports searching among all packages known to the package management tools, installed or otherwise, using regular expressions, without extraneous output, and can be told how to format its output:



          aptitude search "linux-image-.*"


          To list only installed packages:



          aptitude search "linux-image-.* ~i"


          To list only installed package names matching the regular expression:



          aptitude search "linux-image-.* ~i" -F "%p"


          The documentation covers the available search patterns and output format specifiers in detail. You’ll also find examples on this site, for example is there a way to use regexp with aptitude?, regexp with aptitude part 2, and Linux - display or upgrade security updates only using apt.






          share|improve this answer

































            2














            Here's one good way to do get the list of installed packages on a Debian-based system:



            dpkg -l | grep ^ii | awk '{print $2}'


            The output lines of dpkg -l can be trusted to be sane.
            The pattern ^ii will match the lines of installed packages,
            and the simple Awk will extract the second column,
            the package names (the same names used in apt-get install commands).
            Package names cannot contain whitespace,
            so this again is a safe operation.






            share|improve this answer































              1














              $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1{print $1}'
              linux-image-3.16.0-4-amd64
              linux-image-4.11.0-1-amd64
              linux-image-4.12.0-1-amd64
              linux-image-4.13.0-1-amd64
              linux-image-4.8.0-2-amd64
              linux-image-4.9.0-1-amd64
              linux-image-4.9.0-2-amd64
              linux-image-4.9.0-3-amd64


              Talking about regex:



              $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1 && $0~/4.1/{print $1}'
              linux-image-4.11.0-1-amd64
              linux-image-4.12.0-1-amd64
              linux-image-4.13.0-1-amd64


              You can also use dpkg-query with -f (--showformat) option,which when invoked without any package name, by default only installed packages are listed.



              $ dpkg-query -f '${Package}n' -W |grep 'linux-image' #-W == --show





              share|improve this answer

































                0














                In order to "trap" the searched term when grep ping dpkg output, need to encase the search term as follows. "git" is used as the specimen search term:



                dpkg -l |grep "^ii  git[[:space:]]"


                The carat (^) ii followed by (2) spaces prepending the searched term ensures nothing BEFORE it other than that combination of characters can match.



                The [[:space:]] abutting the searched term precludes partial matches from occurring by only matching spaces immediately AFTER it.






                share|improve this answer

























                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "106"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411472%2fright-way-to-get-the-list-of-installed-packages-matching-a-pattern%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  7














                  aptitude supports searching among all packages known to the package management tools, installed or otherwise, using regular expressions, without extraneous output, and can be told how to format its output:



                  aptitude search "linux-image-.*"


                  To list only installed packages:



                  aptitude search "linux-image-.* ~i"


                  To list only installed package names matching the regular expression:



                  aptitude search "linux-image-.* ~i" -F "%p"


                  The documentation covers the available search patterns and output format specifiers in detail. You’ll also find examples on this site, for example is there a way to use regexp with aptitude?, regexp with aptitude part 2, and Linux - display or upgrade security updates only using apt.






                  share|improve this answer






























                    7














                    aptitude supports searching among all packages known to the package management tools, installed or otherwise, using regular expressions, without extraneous output, and can be told how to format its output:



                    aptitude search "linux-image-.*"


                    To list only installed packages:



                    aptitude search "linux-image-.* ~i"


                    To list only installed package names matching the regular expression:



                    aptitude search "linux-image-.* ~i" -F "%p"


                    The documentation covers the available search patterns and output format specifiers in detail. You’ll also find examples on this site, for example is there a way to use regexp with aptitude?, regexp with aptitude part 2, and Linux - display or upgrade security updates only using apt.






                    share|improve this answer




























                      7












                      7








                      7







                      aptitude supports searching among all packages known to the package management tools, installed or otherwise, using regular expressions, without extraneous output, and can be told how to format its output:



                      aptitude search "linux-image-.*"


                      To list only installed packages:



                      aptitude search "linux-image-.* ~i"


                      To list only installed package names matching the regular expression:



                      aptitude search "linux-image-.* ~i" -F "%p"


                      The documentation covers the available search patterns and output format specifiers in detail. You’ll also find examples on this site, for example is there a way to use regexp with aptitude?, regexp with aptitude part 2, and Linux - display or upgrade security updates only using apt.






                      share|improve this answer















                      aptitude supports searching among all packages known to the package management tools, installed or otherwise, using regular expressions, without extraneous output, and can be told how to format its output:



                      aptitude search "linux-image-.*"


                      To list only installed packages:



                      aptitude search "linux-image-.* ~i"


                      To list only installed package names matching the regular expression:



                      aptitude search "linux-image-.* ~i" -F "%p"


                      The documentation covers the available search patterns and output format specifiers in detail. You’ll also find examples on this site, for example is there a way to use regexp with aptitude?, regexp with aptitude part 2, and Linux - display or upgrade security updates only using apt.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 17 '17 at 21:51

























                      answered Dec 17 '17 at 21:44









                      Stephen KittStephen Kitt

                      174k24398473




                      174k24398473

























                          2














                          Here's one good way to do get the list of installed packages on a Debian-based system:



                          dpkg -l | grep ^ii | awk '{print $2}'


                          The output lines of dpkg -l can be trusted to be sane.
                          The pattern ^ii will match the lines of installed packages,
                          and the simple Awk will extract the second column,
                          the package names (the same names used in apt-get install commands).
                          Package names cannot contain whitespace,
                          so this again is a safe operation.






                          share|improve this answer




























                            2














                            Here's one good way to do get the list of installed packages on a Debian-based system:



                            dpkg -l | grep ^ii | awk '{print $2}'


                            The output lines of dpkg -l can be trusted to be sane.
                            The pattern ^ii will match the lines of installed packages,
                            and the simple Awk will extract the second column,
                            the package names (the same names used in apt-get install commands).
                            Package names cannot contain whitespace,
                            so this again is a safe operation.






                            share|improve this answer


























                              2












                              2








                              2







                              Here's one good way to do get the list of installed packages on a Debian-based system:



                              dpkg -l | grep ^ii | awk '{print $2}'


                              The output lines of dpkg -l can be trusted to be sane.
                              The pattern ^ii will match the lines of installed packages,
                              and the simple Awk will extract the second column,
                              the package names (the same names used in apt-get install commands).
                              Package names cannot contain whitespace,
                              so this again is a safe operation.






                              share|improve this answer













                              Here's one good way to do get the list of installed packages on a Debian-based system:



                              dpkg -l | grep ^ii | awk '{print $2}'


                              The output lines of dpkg -l can be trusted to be sane.
                              The pattern ^ii will match the lines of installed packages,
                              and the simple Awk will extract the second column,
                              the package names (the same names used in apt-get install commands).
                              Package names cannot contain whitespace,
                              so this again is a safe operation.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 17 '17 at 21:32









                              janosjanos

                              7,21222447




                              7,21222447























                                  1














                                  $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1{print $1}'
                                  linux-image-3.16.0-4-amd64
                                  linux-image-4.11.0-1-amd64
                                  linux-image-4.12.0-1-amd64
                                  linux-image-4.13.0-1-amd64
                                  linux-image-4.8.0-2-amd64
                                  linux-image-4.9.0-1-amd64
                                  linux-image-4.9.0-2-amd64
                                  linux-image-4.9.0-3-amd64


                                  Talking about regex:



                                  $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1 && $0~/4.1/{print $1}'
                                  linux-image-4.11.0-1-amd64
                                  linux-image-4.12.0-1-amd64
                                  linux-image-4.13.0-1-amd64


                                  You can also use dpkg-query with -f (--showformat) option,which when invoked without any package name, by default only installed packages are listed.



                                  $ dpkg-query -f '${Package}n' -W |grep 'linux-image' #-W == --show





                                  share|improve this answer






























                                    1














                                    $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1{print $1}'
                                    linux-image-3.16.0-4-amd64
                                    linux-image-4.11.0-1-amd64
                                    linux-image-4.12.0-1-amd64
                                    linux-image-4.13.0-1-amd64
                                    linux-image-4.8.0-2-amd64
                                    linux-image-4.9.0-1-amd64
                                    linux-image-4.9.0-2-amd64
                                    linux-image-4.9.0-3-amd64


                                    Talking about regex:



                                    $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1 && $0~/4.1/{print $1}'
                                    linux-image-4.11.0-1-amd64
                                    linux-image-4.12.0-1-amd64
                                    linux-image-4.13.0-1-amd64


                                    You can also use dpkg-query with -f (--showformat) option,which when invoked without any package name, by default only installed packages are listed.



                                    $ dpkg-query -f '${Package}n' -W |grep 'linux-image' #-W == --show





                                    share|improve this answer




























                                      1












                                      1








                                      1







                                      $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1{print $1}'
                                      linux-image-3.16.0-4-amd64
                                      linux-image-4.11.0-1-amd64
                                      linux-image-4.12.0-1-amd64
                                      linux-image-4.13.0-1-amd64
                                      linux-image-4.8.0-2-amd64
                                      linux-image-4.9.0-1-amd64
                                      linux-image-4.9.0-2-amd64
                                      linux-image-4.9.0-3-amd64


                                      Talking about regex:



                                      $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1 && $0~/4.1/{print $1}'
                                      linux-image-4.11.0-1-amd64
                                      linux-image-4.12.0-1-amd64
                                      linux-image-4.13.0-1-amd64


                                      You can also use dpkg-query with -f (--showformat) option,which when invoked without any package name, by default only installed packages are listed.



                                      $ dpkg-query -f '${Package}n' -W |grep 'linux-image' #-W == --show





                                      share|improve this answer















                                      $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1{print $1}'
                                      linux-image-3.16.0-4-amd64
                                      linux-image-4.11.0-1-amd64
                                      linux-image-4.12.0-1-amd64
                                      linux-image-4.13.0-1-amd64
                                      linux-image-4.8.0-2-amd64
                                      linux-image-4.9.0-1-amd64
                                      linux-image-4.9.0-2-amd64
                                      linux-image-4.9.0-3-amd64


                                      Talking about regex:



                                      $ apt list --installed "linux-image-*" 2>/dev/null |awk -F'/' 'NR>1 && $0~/4.1/{print $1}'
                                      linux-image-4.11.0-1-amd64
                                      linux-image-4.12.0-1-amd64
                                      linux-image-4.13.0-1-amd64


                                      You can also use dpkg-query with -f (--showformat) option,which when invoked without any package name, by default only installed packages are listed.



                                      $ dpkg-query -f '${Package}n' -W |grep 'linux-image' #-W == --show






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Dec 17 '17 at 22:07

























                                      answered Dec 17 '17 at 21:38









                                      George VasiliouGeorge Vasiliou

                                      5,69531029




                                      5,69531029























                                          0














                                          In order to "trap" the searched term when grep ping dpkg output, need to encase the search term as follows. "git" is used as the specimen search term:



                                          dpkg -l |grep "^ii  git[[:space:]]"


                                          The carat (^) ii followed by (2) spaces prepending the searched term ensures nothing BEFORE it other than that combination of characters can match.



                                          The [[:space:]] abutting the searched term precludes partial matches from occurring by only matching spaces immediately AFTER it.






                                          share|improve this answer






























                                            0














                                            In order to "trap" the searched term when grep ping dpkg output, need to encase the search term as follows. "git" is used as the specimen search term:



                                            dpkg -l |grep "^ii  git[[:space:]]"


                                            The carat (^) ii followed by (2) spaces prepending the searched term ensures nothing BEFORE it other than that combination of characters can match.



                                            The [[:space:]] abutting the searched term precludes partial matches from occurring by only matching spaces immediately AFTER it.






                                            share|improve this answer




























                                              0












                                              0








                                              0







                                              In order to "trap" the searched term when grep ping dpkg output, need to encase the search term as follows. "git" is used as the specimen search term:



                                              dpkg -l |grep "^ii  git[[:space:]]"


                                              The carat (^) ii followed by (2) spaces prepending the searched term ensures nothing BEFORE it other than that combination of characters can match.



                                              The [[:space:]] abutting the searched term precludes partial matches from occurring by only matching spaces immediately AFTER it.






                                              share|improve this answer















                                              In order to "trap" the searched term when grep ping dpkg output, need to encase the search term as follows. "git" is used as the specimen search term:



                                              dpkg -l |grep "^ii  git[[:space:]]"


                                              The carat (^) ii followed by (2) spaces prepending the searched term ensures nothing BEFORE it other than that combination of characters can match.



                                              The [[:space:]] abutting the searched term precludes partial matches from occurring by only matching spaces immediately AFTER it.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Feb 15 at 19:53

























                                              answered Feb 15 at 18:33









                                              F1LinuxF1Linux

                                              388




                                              388






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411472%2fright-way-to-get-the-list-of-installed-packages-matching-a-pattern%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                                                  is 'sed' thread safe

                                                  How to make a Squid Proxy server?