버전 번호가 낮은 파일 삭제

버전 번호가 낮은 파일 삭제

다음 파일이 있습니다.

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 스크립트를 사용할 수 있습니까? 아니면 어?

답변1

이는 GNU ls+ awk원라이너를 통해 수행할 수 있습니다.

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

설명: 파일 이름이 awk 스크립트에 입력으로 전달됩니다. 이러한 옵션을 -vr사용하면 파일 이름이 버전 번호별로 역순으로 정렬됩니다. 즉, foo-1.9.depotawk foo-1.10.depot스크립트는 이름의 첫 번째 부분(첫 번째 부분까지 -)을 변수에 저장합니다 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출력 이 마음 print에 들면system

관련 정보