脚本:重命名并删除重复的目录

时间:2021-02-21 01:06:28

I have a directory structure with different subdirectories are tailed with version names like

我有一个不同子目录的目录结构与版本名称一样

/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

I need to rename /drivers/i2c_v3 with /drivers/i2c and remove drivers/i2c_v* and /drivers/spi_v2_1 with /drivers/spi and remove /drivers/spi_v*

我需要使用/ drivers / i2c重命名/ drivers / i2c_v3并使用/ drivers / spi删除drivers / i2c_v *和/ drivers / spi_v2_1并删除/ drivers / spi_v *

LATEST VERSION NAME SHOULD RENAME WITH VERSION LESS AND REMAINING VERSION-ED ONE'S SHOULD DELETE.

最新版本的名称应该重复版本,并且版本保持不变 - 一个人应该删除。

Any help...

任何帮助......

2 个解决方案

#1


1  

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

#2


0  

Try something like this, if your filenames sorts correctly with sort or sort -n

如果您的文件名使用sort或sort -n正确排序,请尝试这样的操作

fn=$( ls i2c* | sort -r | head -n1 ); mv -f "$fn" i2c; rm -f i2c_*

#1


1  

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

#2


0  

Try something like this, if your filenames sorts correctly with sort or sort -n

如果您的文件名使用sort或sort -n正确排序,请尝试这样的操作

fn=$( ls i2c* | sort -r | head -n1 ); mv -f "$fn" i2c; rm -f i2c_*