Сценарий Bash - удалить все файлы старше, но оставить их по соображениям безопасности

С другой стороны, рассмотрели ли вы использование альтернативного менеджера wifi? Я сам имел некоторые проблемы с нм-апплет, но я установил Wicd из хранилищ, и проблемы просто исчезли.

Вы можете получить Wicd [F1], а затем перезапустить X.

3
задан 27 July 2017 в 11:01

2 ответа

Вот один из способов:

#!/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]}")

Предостережения:

Предполагается, что у вас есть только директории в /opt/bla/myfiles. Он удалит все, кроме каталога последней версии.
1
ответ дан 18 July 2018 в 09:37

Вот один из способов:

#!/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]}")

Предостережения:

Предполагается, что у вас есть только директории в /opt/bla/myfiles. Он удалит все, кроме каталога последней версии.
1
ответ дан 24 July 2018 в 19:23
  • 1
    эй, спасибо за ответ, я постараюсь проверить как можно скорее. Решение этого массива выглядит многообещающим. – Prologas 27 July 2017 в 15:04

Другие вопросы по тегам:

Похожие вопросы: