37

I want to uninstall libreoffice. This program consists of about three dozen modules. Ideally, they could be removed with:

aptitude remove libreoffice3.6* libreoffice-debian-menus libobasis3.6-*

but that fails with

Couldn't find any package whose name or description matched "libreoffice3.6*"

etc.

How do I delete a set of packages by pattern?

PS: I'm happy about answers with use dpkg or apt, too

Braiam
  • 67,791
  • 32
  • 179
  • 269

5 Answers5

48
  1. Use apt-get or apt, (not aptitude), and use anchored regular expressions.

  2. In a regular expression, . mean any character, and * means zero or more times. So the expression libreoffice.* matches any package name containing the string libreoffice, followed by any number of characters. You must anchor the regular expression with ^ (to match the beginning of the string) or $ (to match the end of the string) or both, otherwise, the regular expression will not be recognised by APT.

  3. Surround the regular expression with single quotes to avoid the shell interpreting the asterisk.

Example:

To remove all packages with a name that starts with libreoffice, run:

sudo apt remove '^libreoffice.*$'
Flimm
  • 41,766
  • Thanks, this works. Interestingly enough, it also works with the pattern libreoffice* (i.e. glob style patterns) which confuses me a bit because either of them shouldn't work :-) – Aaron Digulla Apr 04 '13 at 12:09
  • 4
    For those who want to test this, use the option --dry-run to see what would be deleted without any changes to the system. – Aaron Digulla Apr 04 '13 at 12:11
  • apt-get is pretty bad handling regular expressions too, compare both solutions in this answer. – Braiam Mar 31 '14 at 02:42
  • 1
    To make it clear, apt uses POSIX regular expressions. From man apt-get (Debian Jessie): "If no package matches the given expression and the expression contains one of '.', '?' or '*' then it is assumed to be a POSIX regular expression..." – x-yuri Nov 20 '17 at 16:07
  • I've updated the answer to say that the regular expression must be anchored. From the man page: "Fallback to regular expressions is deprecated in APT 2.0, has been removed in apt(8), except for anchored expressions, and will be removed from apt-get(8) in a future version. Use apt-patterns(5) instead." – Flimm Jul 09 '23 at 11:16
  • apt-get does seem to support globs (sudo apt remove 'libreoffice*' notice the lack of a dot), but I can't seem to find documentation about this. @AaronDigulla – Flimm Jul 09 '23 at 11:17
12

An alternative is:

dpkg -l | grep libreoffice | awk '{print $2}' | xargs -n1 echo

This will list out all the packages matching libreoffice. When you've confirmed that they're all the ones you wish to get rid of, run the following command... with caution:

dpkg -l | grep libreoffice | awk '{print $2}' | xargs -n1 sudo apt-get purge -y

The idea:

  1. Get the system to list out all installed packages
  2. Filter to show only the ones matching libreoffice
  3. Filter to show only the column with the package name
  4. Run the purge command on each of those packages
aalaap
  • 408
  • 1
    maybe you could suggest adding the -p option so that xargs will prompt for confirmation before executing each command constructed, or first checking with echo instead of sudo apt-get purge – Zanna Oct 18 '16 at 06:11
  • 1
    @Zanna -p would help, but it wouldn't be a one-shot command. I did use echos to test what I was getting before running the command, so that's worth recommending. – aalaap Oct 18 '16 at 06:21
  • 2
    I can give you +1 now you made it safer :) – Zanna Oct 18 '16 at 06:31
  • I think the -n1 is good for the echo but you have to remove it from the purge; otherwise, dependency order (a depends on b, tries to delete b first) might break the purge. – Aaron Digulla Nov 09 '16 at 13:01
  • Your solution is the best I've found until now, thank you. – João Pedro Nov 03 '17 at 13:39
  • This is great! However, for those who, like myself, are having issues with apt itself, dpkg may be used as the final command, like so: dpkg -l | grep libreoffice | awk '{print $2}' | xargs -n1 sudo dpkg -r (or with sudo dpkg -P to purge instead of remove). – justathoughtor2 Aug 25 '19 at 05:34
6

Aptitude has support for global patterns, and another pretty cool matches like this:

aptitude remove '?and(?name(libreoffice), name(3.6), ~i)' libreoffice-debian-menus

This will match any package that has in it's name libreoffice and 3.6 and also it's installed (that's what the ~i stands for.

Braiam
  • 67,791
  • 32
  • 179
  • 269
4

2023 solution

sudo apt remove 'libreoffice*' -y

(The -y flag skips the "are you sure?" prompt)

birgersp
  • 441
  • 1
  • 7
  • 19
1

When you need to remove many files having the same prefix, I find brace expansion very handy:

sudo apt remove libreoffice-l10n-{bg,ca,cs,da,de,en-za,es,fr,hu,id,ja,ko,nb,nl,pl,pt,ru,sv,th,tr,uk,vi,zh-cn,zh-tw}

I used this command to remove all the language packs that I never use. Yes, with a regex you can tell which one to keep, and delete the others. Anyway I like this because it's easy to remember, and it works also with many bash commands.

funder7
  • 196