ディレクトリを階層に再構成する

ディレクトリを階層に再構成する

数千のフォルダがある次の種類のフォルダ構造があります。

フォルダ名自体が異なる名前などを持っています。

.test
.test.subfolder
.test.subfolder.subsubfolder

.folder
.folder.one
.folder.two
.folder.one.one

.test私が達成しなければならないことは次のとおりです。たとえば、フォルダの名前をfromに変更し、testフォルダ.test.subfolderがフォルダなしでフォルダ内にあるように.subfolder移動し、そのフォルダの内側と内側になるようにします。test..test.subfolder.subsubfoldersubfoldertestsubsubfoldertest/subfolder

フォルダが多いので再帰が必要です。フォルダ内のファイルはそのまま残す必要があることを覚えておいてください。

これは可能ですか?

ベストアンサー1

このbashスクリプトは必要なタスクを実行します。

#!/bin/bash
for dir in .*/ ; do
    [[ $dir == ./ || $dir == ../ ]] && continue  # Skip the special dirs
    new=${dir#.}                                 # Remove the dot at the beginning
    new=./${new//.//}                            # Replace dots with slashes, prepend ./
    new=${new%/}                                 # Remove the trainling slash
    mkdir -p ${new%/*}                           # Create the parent dir
    mv "$dir" "$new"                             # Move the dir to destination
done

おすすめ記事