查找最新的文件夹/文件版本并删除 - 无时间戳数据

查找最新的文件夹/文件版本并删除 - 无时间戳数据

我从备份中恢复了数据,并且恢复的数据具有相同文件夹/文件的多个版本。文件夹/文件已在文件和文件夹上以 _000 _001 _002 等命名。所有修改的时间戳都是相同的。所以文件夹/文件看起来像这样

[PATH  ~/Folder/9] (VERSION 1) EMPTY - and can be deleted  
[PATH  ~/Folder/9_000] (VERSION 2) EMPTY - and can be deleted  
[PATH  ~/Folder/9_001] (VERSION 3) Data is in this last one  

FILEA.TXT (VERSION 1) remove/delete this one  
FILEA_000.TXT (VERSION 2) remove/delete this one  
FILEA_001.TXT (VERSION 3) remove/delete this one  
FILEA_002.TXT (VERSION 4) I need to keep this one and then rename  

FILEB.TXT (VERSION 1) remove/delete this one  
FILEB_000.TXT (VERSION 2) I need to keep this one and then rename  

其中一些文件夹的深度超过 6 个。我发现的模式是每个文件夹/文件的最后一个文件或文件夹版本是好的版本,不需要的文件夹是空的。不好的是并非所有文件/文件夹都具有相同数量的版本。所以仅仅因为 FILEA.TXT 有 4 个版本(FILEA.TXT 到 FILEA_002.TXT),FILEB.TXT 只有 2 个版本(FILEB.TXT 和 FILEB_000.TXT)。所以我无法搜索所有文件并_002移动或删除。我可以轻松地使用 bash ( find . -type d -empty -delete) 删除所有不需要的空文件夹,这解决了我在文件夹上的部分问题,除了重命名之外。

我需要删除所有不需要的文件,然后删除文件夹/文件上的版本号,以便我的软件可以正确导入恢复的数据。以下是路径以及路径中的文件和文件夹的示例。

[PATH  ~/Folder/9] EMPTY - and can be deleted  
[PATH  ~/Folder/9_000] EMPTY - and can be deleted  
[PATH  ~/Folder/9_001] Data is in this last one  

[PATH  ~/Folder/9_001/62BF7CA1] EMPTY - and can be deleted  
[PATH  ~/Folder/9_001/62BF7CA1_000] EMPTY - and can be deleted  
[PATH  ~/Folder/9_001/62BF7CA1_001] Data is in the last one  

[ FOLDERS/FILES IN "~/Folder/9_001/62BF7CA1_001" ]  

Archive - unwanted deleted it  
Archive_000 - unwanted deleted it  
Archive_001 - unwanted deleted it  
Archive_002 - unwanted deleted it  
Archive_003 - keep  
Documents - unwanted deleted it  
Documents_000 - keep
FolderX - unwanted deleted it  
FolderX_000 - unwanted deleted it  
FolderX_001 - unwanted deleted it  
FolderX_002 - keep
62BF7CA1.PDF - unwanted deleted it  
62BF7CA1_000.PDF - unwanted deleted it  
62BF7CA1_001.PDF - unwanted deleted it  
62BF7CA1_002.PDF - keep  
62BF7CA1.TXT- keep
62BF7CA1.DOC - unwanted deleted it  
62BF7CA1_000.DOC - unwanted deleted it  
62BF7CA1_001.DOC - unwanted deleted it  
62BF7CA1_002.DOC - keep  
62BF7CA1.QIF - unwanted deleted it  
62BF7CA1_000.QIF - unwanted deleted it  
62BF7CA1_001.QIF - unwanted deleted it  
62BF7CA1_002.QIF - keep  

该路径中要保留的文件和文件夹是

Archive_003  
Documents_000  
FolderX_002  
62BF7CA1_002.PDF  
62BF7CA1.TXT  
62BF7CA1_002.DOC  
62BF7CA1_002.QIF  

...然后我需要删除任何 _000 _001 _002 等(如果每个文件夹中存在)

Archive  
Documents  
FolderX  
62BF7CA1.PDF  
62BF7CA1.TXT  
62BF7CA1.DOC  
62BF7CA1.QIF  

使用 sed 和管道之类的东西可能很容易,但我不知道。

答案1

迭代所有文件。找到也与该组件匹配的那些_nnn。确定最后一个并删除其他的。

如果您需要将其应用于文件夹树,一种选择是将其放入脚本中并从构造中调用该脚本find -type d -exec

for file in *.*
do
    [[ -d "$file" || $file =~ _[[:digit:]]{3}\. ]] && continue
    echo -n "Considering $file: " >&2

    extn="${file/*.}"
    versions=("$file")
    keep="$file"

    # Look at matching files
    for version in "${file%.$extn}"_???."$extn"
    do
        [[ -f "$version" ]] || continue

        # Save every one. Identify the current last
        versions+=("$version")
        keep="$version"
        echo -n "$version " >&2
    done
    echo "==> keep $keep" >&2

    # Delete them all except the last
    for version in "${versions[@]}"
    do
        [[ "$version" != "$keep" ]] && echo rm -f -- "$version"
    done
    [[ "$keep" != "$file" ]] && echo mv -f -- "$keep" "$file"
done

删除echo前面的语句rm -f -- "$version",当您觉得舒服时mv -f -- "$keep" "$file",它会删除您想要删除的文件并保留您想要保留的文件。

相关内容