4

I have these files:

atk-2.8.0-ia64-11.31.depot           gtk+2-2.24.25-ia64-11.31.depot      lzo-2.09-ia64-11.31.depot
bash-4.3.030-ia64-11.31.depot        gtk+3-3.6.5-ia64-11.31.depot        m4-1.4.17-ia64-11.31.depot
bash-4.3.033-ia64-11.31.depot        harfbuzz-0.9.35-ia64-11.31.depot    make-4.1-ia64-11.31.depot
bzip2-1.0.6-ia64-11.31.depot         harfbuzz-0.9.36-ia64-11.31.depot    Mesa-7.4.4-ia64-11.31.depot
cairo-1.14.0-ia64-11.31.depot        harfbuzz-0.9.37-ia64-11.31.depot    ncurses-5.9-ia64-11.31.depot
coreutils-8.23-ia64-11.31.depot      harfbuzz-0.9.38-ia64-11.31.depot    nettle-2.7.1-ia64-11.31.depot
cups-1.7.5-ia64-11.31.depot          heimdal-1.5.2-ia64-11.31.depot      openldap-2.4.40-ia64-11.31.depot
cyrus_sasl-2.1.26-ia64-11.31.depot   installed_packages.txt              openssl-1.0.1j-ia64-11.31.depot
db-6.0.20-ia64-11.31.depot           jasper-1.900.1-ia64-11.31.depot     openssl-1.0.1k-ia64-11.31.depot
editline-2.9-ia64-11.31.depot        jpeg-9.1.0-ia64-11.31.depot         openssl-1.0.1l-ia64-11.31.depot
expat-2.1.0-ia64-11.31.depot         lcms2-2.6-ia64-11.31.depot          openssl-1.0.2-ia64-11.31.depot
findutils-4.4.2-ia64-11.31.depot     lcms-1.19-ia64-11.31.depot          p11_kit-0.22.1-ia64-11.31.depot
flex-2.5.39-ia64-11.31.depot         ldns-1.6.17-ia64-11.31.depot        packages-ia64-11.31.gz
fontconfig-2.11.1-ia64-11.31.depot   libcroco-0.6.8-ia64-11.31.depot     pango-1.36.8-ia64-11.31.depot
freetype-2.5.3-ia64-11.31.depot      libffi-3.1-ia64-11.31.depot         pcre-8.36-ia64-11.31.depot
freetype-2.5.5-ia64-11.31.depot      libffi-3.2.1-ia64-11.31.depot       pixman-0.32.6-ia64-11.31.depot
gdk_pixbuf-2.28.2-ia64-11.31.depot   libgcrypt-1.6.2-ia64-11.31.depot    popt-1.16-ia64-11.31.depot
gettext-0.19.3-ia64-11.31.depot      libgpg_error-1.17-ia64-11.31.depot  readline-6.3.008-ia64-11.31.depot
gettext-0.19.4-ia64-11.31.depot      libgpg_error-1.18-ia64-11.31.depot  rsync-3.1.1-ia64-11.31.depot
ghostscript-9.06.0-ia64-11.31.depot  libgsf-1.14.30-ia64-11.31.depot     tar-1.28-ia64-11.31.depot
ghostscript-9.14.0-ia64-11.31.depot  libiconv-1.14-ia64-11.31.depot      termcap-1.3.1-ia64-11.31.depot
glib2-2.34.3-ia64-11.31.depot        libidn-1.29-ia64-11.31.depot        tiff-4.0.3-ia64-11.31.depot
gmp-5.0.5-ia64-11.31.depot           libpng-1.6.8-ia64-11.31.depot       unbound-1.4.22-ia64-11.31.depot
gnutls-3.3.10-ia64-11.31.depot       librsvg-2.40.5-ia64-11.31.depot     unbound-1.5.0-ia64-11.31.depot
gnutls-3.3.11-ia64-11.31.depot       libtasn1-4.2-ia64-11.31.depot       unbound-1.5.1-ia64-11.31.depot
gnutls-3.3.12-ia64-11.31.depot       libXft-2.3.2-ia64-11.31.depot       vim-7.4-ia64-11.31.depot
gnutls-3.3.9-ia64-11.31.depot        libxml2-2.9.1-ia64-11.31.depot      xz-5.0.7-ia64-11.31.depot
grep-2.20-ia64-11.31.depot           libxml2-2.9.2-ia64-11.31.depot      xz-5.2.0-ia64-11.31.depot
grep-2.21-ia64-11.31.depot           libXrender-0.9.8-ia64-11.31.depot   zlib-1.2.8-ia64-11.31.depot
gtk+2-2.24.24-ia64-11.31.depot       lzo-2.08-ia64-11.31.depot

Some of them have older version numbers, I want to remove all duplicate files with minor version numbers. Is it possible with a bash script? Or awk?

2
  • 1
    Any reason you aren't using your package manager for this?
    – jasonwryan
    Feb 16, 2015 at 19:44
  • Yes command line and multiple hosts
    – elbarna
    Feb 16, 2015 at 20:35

2 Answers 2

6

It can be done by GNU ls+awk one-liner:

ls -vr *.depot | awk -F- '$1 == name{system ("rm \""$0"\"")}{name=$1}'

Explanation: the file names are passed as input to the awk script. The options -vr cause the file names to be sorted as version numbers in reverse order, so e.g. foo-1.9.depot comes after foo-1.10.depot. The awk script stores the first part of the name (up to the first -) in the variable name. When the first part of the current name is identical to the first part of the previous name, the script invoked rm to delete the current file (which is an older version).

3
  • can you explain this please ?
    – klerk
    Feb 16, 2015 at 21:19
  • @Gilles Thanx, I am late a little with explanation. ;)
    – Costas
    Feb 16, 2015 at 21:50
  • Cool! I didn't knew ls -v (+i)
    – JJoao
    Feb 16, 2015 at 22:08
2
$ cat remover
#!/usr/bin/perl

for(<*depot>){
  if(/(\w.*?)-(\d+)(?:\.(\d+))?\.(\d+)-/){
     $norm=sprintf("%04d%04d%04d",$2,$3,$4);
     if($v{$1} and $v{$1} > $norm){ unlink($_) ; next }
     if($v{$1}                   ){ unlink($name{$1}) }
     $v{$1}=$norm;
     $name{$1}=$_;
  }
}

try perl remover and replace print by system if you like the output

4
  • Why is this being downvoted?
    – JJoao
    Feb 17, 2015 at 10:22
  • +1 to right a wrong - No matter how much more elegant the other solution is, and especially because you posted first, your solution should not have been downvoted. Downvoting usually calls for some form of justification or obvious reason.
    – Cbhihe
    Jun 22, 2015 at 7:00
  • 1
    Why are you using "system" (which many consider inherently bad and which should always be avoided unless there is no alternative) and not using "unlink" to remove the file?
    – J G Miller
    Sep 26, 2016 at 17:54
  • @JGMiller, thank you for the suggestion: updated. Unlink is better of course, but system is not exactly a crime :) I often do a debugging step where I just print the action to do (rm 'file') and substitute print by system.
    – JJoao
    Sep 26, 2016 at 21:44

You must log in to answer this question.

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