AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 940038
Accepted
pr0logas
pr0logas
Asked: 2017-07-27 08:13:32 +0800 CST2017-07-27 08:13:32 +0800 CST 2017-07-27 08:13:32 +0800 CST

Bash 脚本 - 删除所有早于的文件,但出于安全原因保留一个

  • 772

我写了一个脚本:

#!/bin/bash
dir=/opt/bla/myfiles
# Check disk usage usep=$(df -H | grep /dev/sda3 | awk '{print $5}' | cut -d '%' -f1)   if [ $usep -ge 90 ]; then
    echo "$(date) Running out of space in /dev/sda3 with $usep percent - so deleting action is taking!" >> /var/log/messages &&
        find $dir/releases/* -mtime +3 -exec rm {} \; else
        echo "$(date) Disk space is $usep percent - no action required!" /var/log/messages   fi

效果很好。但我现在需要更高级的方法。如您所见,它会删除 dir 中超过 3 天的所有文件。我有很多版本,例如:1.31.1 1.31.2 1.31.3 ...... 1.31.150 1.32.1 1.32.2

等等。我想从主要版本 1.31/1.32 中删除除最后一个版本之外的所有版本。如何?它不能是静态名称,因为有一天它会是 2.32.150

ls -l
total 520
drwxr-xr-x 2 jenkins jenkins 4096 Jun 23 15:45 0.0.31-SNAPSHOT
drwxr-xr-x 2 jenkins jenkins 4096 Jun 23 15:45 1.33.0.100-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun 23 15:45 1.33.0.101-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun  8 11:00 1.33.0.58-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun  8 11:00 1.33.0.59-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun  8 11:00 1.33.0.64-RELEASE
drwxr-xr-x 2 jenkins jenkins 4096 Jun  8 11:00 1.33.0.66-RELEASE

任何想法,谢谢!

bash
  • 1 1 个回答
  • 1619 Views

1 个回答

  • Voted
  1. Best Answer
    terdon
    2017-07-28T00:54:46+08:002017-07-28T00:54:46+08:00

    这是一种方法:

    #!/bin/bash
    
    targetDir=/opt/bla/myfiles;
    ## declare 'releases' as an associative array
    declare -A releases
    cd "$targetDir"
    ## Iterate over all directories in $targetDir. 
    for dir in */; do
            ## remove the trailing slash
            dir="${dir%/}"
            ## Extract the version string
            ver="${dir%%-*}"
    
            ## Use the version as the key for the associative array 
            releases["$ver"]="$dir";
    done
    ## Get the newest version; sort -h understands version numbers
    newestVersion=$( printf '%s\n' "${!releases[@]}" | sort -h | tail -n1)
    ## This is probably not needed as extended globbing should be on by default
    shopt -s extglob
    ## Delete the rest. The '$targetDir/' isn't necessary but it's safer
    ## just in case we're not actually where we think we are. 
    rm -rf  $targetDir/!("${releases[$newestVersion]}")
    

    注意事项:

    1. 这假设您只有/opt/bla/myfiles.
    2. 它将删除除最新版本目录之外的所有内容。
    • 1

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve