3

I accidentally deleted all .desktop files from /usr/share/applications directory.

Is there any way to restore/recover/regenerate it back for all the applications that I have currently installed in my Ubuntu 16.04 LTS laptop?

David Foerster
  • 35,754
  • 55
  • 92
  • 145
  • 1
    Possible duplicate of [How to recover deleted files?](https://askubuntu.com/questions/3883/how-to-recover-deleted-files) – derHugo Sep 20 '17 at 16:59
  • 4
    @derHugo: No, there are fare better (narrower and more likely to succeed) methods. See my answer. – David Foerster Sep 20 '17 at 17:56

1 Answers1

8

Since only files extracted from packages are supposed be stored in /usr (but not /usr/local) you can use the package manager to recover all those files:

  • dpkg-query -S [PATH] lists all currently installed packages that (used to) have files in [PATH].

  • With a bit of mangling we can pass the package names to apt-get to reinstall them:

    xargs -r -d '\n' -a <(
        dpkg-query -S /usr/share/applications/ <&- |
        gawk -F '[,:][ \t]' '{ for (i=1; i<NF; i++) if (!a[$i]++) print $i; }'
      ) -- sudo apt-get install --reinstall --
    
  • If there are packages among them that weren't installed from a package repository but manually from a Deb package file, apt-get will complain that it can't find those particular packages and you'll have to install them anew.

David Foerster
  • 35,754
  • 55
  • 92
  • 145
  • As you’ve never accepted an answer before: if this answers your question, don’t forget to click the grey ☑ under the number at the left of this text to [accept it](/help/someone-answers), which means “yes, this answer is valid”! – David Foerster Sep 20 '17 at 18:06
  • +1 to partially balance lack of acceptance. The OP had signed on within the last week so it's not an abandoned question. – WinEunuuchs2Unix Jul 01 '18 at 23:08