删除低版本号文件

删除低版本号文件

我有这些文件:

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

其中一些具有较旧的版本号,我想删除所有具有次要版本号的重复文件。用bash脚本可以吗?还是awk?

答案1

可以通过 GNU ls+ awkone-liner 来完成:

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

说明:文件名作为 awk 脚本的输入传递。这些选项-vr导致文件名按版本号以相反的顺序排序,因此 egfoo-1.9.depot位于foo-1.10.depot. awk 脚本将名称的第一部分(直到第一个-)存储在变量中name。当当前名称的第一部分与先前名称的第一部分相同时,调用脚本rm来删除当前文件(这是旧版本)。

答案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}=$_;
  }
}

如果您喜欢输出,请尝试perl remover替换printsystem

相关内容