Posted: . At: 10:21 AM. This was 12 months ago. Post ID: 17939
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to use a regex to list certain packages on Linux.


Using a regex with grep is a very useful way to list only certain packages. This regex below looks for the word ‘gtk’ with nothing before it, and then a version number after it. This allows me to list all GK packages.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ dnf list installed | grep '^gtk[0-9]-*'
gtk2.x86_64                                      2.24.33-7.el9                       @appstream             
gtk2-engines.x86_64                              2.20.2-24.el9                       @epel                  
gtk3.x86_64                                      3.24.31-2.el9                       @AppStream             
gtk3-devel.x86_64                                3.24.31-2.el9                       @appstream             
gtk4.x86_64                                      4.4.1-2.el9                         @AppStream

This example below shows how the ^ character in a regex defines that nothing is desired before the string we are searching for.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ dnf list installed | grep '^wayland'
wayland-devel.x86_64                             1.19.0-4.el9                        @appstream

This is not too hard.

This is another way to do this.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ dnf list installed | awk '/^gtk/  {print $1}'
gtk-doc.x86_64
gtk-layer-shell.x86_64
gtk-murrine-engine.x86_64
gtk-update-icon-cache.x86_64
gtk-vnc2.x86_64
gtk2.x86_64
gtk2-engines.x86_64
gtk3.x86_64
gtk3-devel.x86_64
gtk4.x86_64
gtkmm30.x86_64
gtksourceview3.x86_64
gtksourceview4.x86_64

This will find all packages that start with ‘gtk’.

Another way to get package information is wih the rpm utility.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ rpm -ql nmap | grep bin
/usr/bin/nmap
/usr/bin/nping
/usr/share/nmap/nselib/bin.lua
/usr/share/nmap/scripts/ip-geolocation-map-bing.nse

Yet another way to find executable files is using the ls command. This will list only executable binary files.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ ls -Fla /usr/local/bin/ | grep '^\S*x\S*'
drwxr-xr-x.  2 root root       34 Mar 22 17:06 ./
drwxr-xr-x. 12 root root      131 Feb  6 13:22 ../
-rwxr-xr-x.  1 root root 15851600 Dec  5 15:48 gzdoom*
-rwxr-xr-x.  1 root root  2747279 Mar  5 09:26 yt-dlp*

Or this version filters the one-liner to only show the desired files.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ ls -Fla /usr/local/bin/ | grep -Ev './|../,^\S*x\S*'
total 18168
-rwxr-xr-x.  1 root root 15851600 Dec  5 15:48 gzdoom*
-rwxr-xr-x.  1 root root  2747279 Mar  5 09:26 yt-dlp*

This regex will filter out certain matches from the grep output.

-Ev './|../'

The | character defines a separator betwixt different terms.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.