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

4
  • Are you trying to know the answer about certain pattern or happy with the removal of LibreOffice components?
    – Anwar
    Apr 4, 2013 at 7:39
  • 2
    possible duplicate of How to uninstall LibreOffice? The answer is probabaly: this only works for 3.6 and you are not using 3.6 :-)
    – Rinzwind
    Apr 4, 2013 at 7:44
  • I'm using libreoffice as an example because it contains so many parts. But I had the same problem with the kernel (cleaning up old versions) Apr 4, 2013 at 12:12
  • @AaronDigulla Yes, but old versions of the kernel at least have some potential usage scenarios, unlike the "loathsome culprit and huge waste of disk space" that is directly mentioned in this question. Nov 16, 2022 at 15:31

5 Answers 5

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.*$'
6
  • 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 :-) Apr 4, 2013 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. Apr 4, 2013 at 12:11
  • apt-get is pretty bad handling regular expressions too, compare both solutions in this answer.
    – Braiam
    Mar 31, 2014 at 2: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, 2017 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 9, 2023 at 11:16
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
6
  • 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, 2016 at 6: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, 2016 at 6:21
  • 2
    I can give you +1 now you made it safer :)
    – Zanna
    Oct 18, 2016 at 6: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. Nov 9, 2016 at 13:01
  • Your solution is the best I've found until now, thank you. Nov 3, 2017 at 13:39
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.

1
  • I think apt-get and apt have support for this now.
    – Flimm
    Jul 9, 2023 at 11:13
4

2023 solution

sudo apt remove 'libreoffice*' -y

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

1
  • Thanks. I edited my answer.
    – birgersp
    Jul 16, 2023 at 15:46
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.

You must log in to answer this question.

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