0

This question already has an answer here:

How do I use the command line interface (not a GUI) to convert the list of dependency packages in the output from apt-cache depends into a space‐separated list for use with apt-get download? Preferably, I’d like the two actions to be executable via a single command.

(By “list of dependency packages”, I’m referring to all packages prefixed by the string “Depends:”.)

The use case is to download all dependent packages of a package onto a machine with Internet access for use on a machine without Internet access as well as to teach myself how to reformat text via the command line.

One example with a long list of dependencies involves the command apt-cache depends gimp, the output of which is shown below. (This example includes a few dependencies listed multiple times, but apt-get download apparently ignores duplicate entries, so there’s no need to filter them out.)

gimp
  Depends: libgimp2.0
  Depends: libgimp2.0
  Depends: gimp-data
  Depends: gimp-data
  Depends: libgdk-pixbuf2.0-0
  Depends: xdg-utils
  Depends: libaa1
  Depends: libbabl-0.1-0
  Depends: libbz2-1.0
  Depends: libc6
  Depends: libcairo2
  Depends: libfontconfig1
  Depends: libfreetype6
  Depends: libgcc1
  Depends: libgegl-0.4-0
  Depends: libgexiv2-2
  Depends: libglib2.0-0
  Depends: libgs9
  Depends: libgtk2.0-0
  Depends: libgudev-1.0-0
  Depends: libharfbuzz0b
  Depends: libheif1
  Depends: libilmbase23
  Depends: libjpeg8
  Depends: liblcms2-2
  Depends: liblzma5
  Depends: libmng2
  Depends: libmypaint-1.3-0
  Depends: libopenexr23
  Depends: libopenjp2-7
  Depends: libpango-1.0-0
  Depends: libpangocairo-1.0-0
  Depends: libpangoft2-1.0-0
  Depends: libpng16-16
  Depends: libpoppler-glib8
  Depends: librsvg2-2
  Depends: libstdc++6
  Depends: libtiff5
  Depends: libwebp6
  Depends: libwebpdemux2
  Depends: libwebpmux3
  Depends: libwmf0.2-7
  Depends: libx11-6
  Depends: libxcursor1
  Depends: libxext6
  Depends: libxfixes3
  Depends: libxmu6
  Depends: libxpm4
  Depends: zlib1g
  Breaks: gimp-plugin-registry
  Recommends: ghostscript
    ghostscript:i386
 |Suggests: gimp-help-en
  Suggests: <gimp-help>
    gimp-help-ca
    gimp-help-de
    gimp-help-el
    gimp-help-en
    gimp-help-es
    gimp-help-fr
    gimp-help-it
    gimp-help-ja
    gimp-help-ko
    gimp-help-nl
    gimp-help-nn
    gimp-help-pt
    gimp-help-ru
    gimp-help-sl
    gimp-help-sv
  Suggests: gimp-data-extras
  Suggests: gimp-python
  Suggests: gvfs-backends
    gvfs-backends:i386
  Suggests: libasound2
    liboss4-salsa-asound2
  Replaces: gimp-plugin-registry

marked as duplicate by N0rbert, Eric Carvalho, Charles Green, Parto, guiverc Mar 27 at 1:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Why would you need that? apt install gimp will automatically take care of any dependencies (even recursively) of that package and download and install them along with gimp. – PerlDuck Mar 21 at 12:22
  • It is not clear what you are asking. apt install gimp automatically installs all dependencies. The only time you may see "depends" in errors is when a dependency is unmet. That is, there is a version conflict. – user68186 Mar 21 at 12:35
  • apt install gimp can’t fulfill dependencies while performing an offline installation hence the mention of apt-get download. – Patrick Dark Mar 21 at 12:37
  • 1
    Ok, that's a valid reason. But maybe you want to have a look at this question: How can I install software or packages without Internet (offline)? – PerlDuck Mar 21 at 12:42
  • Please edit your question and indicate the question is about offline install. – user68186 Mar 22 at 16:56
0

The answer I arrived at is apt-get download $(apt-cache depends gimp | while read line; do grep --only-matching --perl-regexp "(?<= Depends: ).+"; done | tr "\n" " ") using the package gimp as an example.

Explanation:

  • Run apt-cache depends as a subshell argument to apt-get download.
  • On the initial subshell output, use a while loop that that splits the output into lines assigned to the variable line—which can be named anything and is otherwise not required—using the read command.
  • For each line of output, use the grep command with a Perl regular expression. enabled with the flag --perl-regexp, that utilizes a positive lookbehind involving the string “Depends:”, and only display the matched text using the flag --only-matching.
  • Finally, translate all newline characters into space characters using the tr command.

Not the answer you're looking for? Browse other questions tagged or ask your own question.