Posted: . At: 10:21 AM. This was 4 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}'

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

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

Your email address will not be published. Required fields are marked *

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