我有一个目录结构,不同的子目录后面都是版本名
喜欢

/drivers/i2c_v1
/drivers/i2c_v1_0
/drivers/i2c_v2_1
/drivers/i2c_v3
/drivers/spi_v1
/drivers/spi_v2
/drivers/spi_v2_0
/drivers/spi_v2_1

我需要用/drivers/i2c_v3重命名/drivers/i2c并删除drivers/i2c_v*
/drivers/spi_v2_1使用/drivers/spi并删除/drivers/spi_v*
最新的版本名应该用较少的版本重命名,而剩余的版本名应该
删除。
任何帮助。。。

最佳答案

prefixes=$(ls /drivers/*_v* | sed 's/_.*//' | uniq)
for p in $prefixes; do
    all=($(ls -vr ${p}_*))  # Sort in reverse version order
    latest=${all[0]}        # So first one is highest version
    mv $latest /drivers/temp    # Rename it so it won't be deleted by wildcard below
    if [[ ${#all[@]} > 1 ]]; then   # Check that there's more than one version
        rm -rf ${p}_*               # Remove the ones other than latest
    fi
    mv /drivers/temp ${p}   # Rename it to the non-versioned name
done

关于linux - 脚本:重命名并删除重复的目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23216109/

10-11 03:38