我正在嘗試從唯一的文件名建立資料夾。以下是文件:
T001
└── 20000101
├── 6_3D-MP-RAGE_97.dcm
├── 6_3D-MP-RAGE_98.dcm
├── 5_133_VOLUMES_99027.dcm
├── 5_133_VOLUMES_99028.dcm
├── 5_133_VOLUMES_99029.dcm
├── 5_133_VOLUMES_99030.dcm
├── 4_COR_3D_T1_MPR_1.dcm
├── 4_COR_3D_T1_MPR_107.dcm
我希望 bash 魔法能夠產生這樣的結果:
T001
└── 20000101
├── 6_3D
├── 6_3D-MP-RAGE_97.dcm
├── 6_3D-MP-RAGE_98.dcm
├── 5_133
├── 5_133_VOLUMES_99027.dcm
├── 5_133_VOLUMES_99028.dcm
├── 5_133_VOLUMES_99029.dcm
├── 5_133_VOLUMES_99030.dcm
├── 4_COR
├── 4_COR_3D_T1_MPR_1.dcm
├── 4_COR_3D_T1_MPR_107.dcm
我嘗試使用此程式碼來提取資料夾名稱,但卡住了:
all_patterns=()
for file in sorted/folder/*/*
do
name=${file##*/}
pattern=`echo ${name} | cut -f1 -d '_'`
all_patterns+=( "$pattern" )
done
答案1
您可以嘗試使用此程式碼來產生目錄名稱並建立這些目錄:
cd T001/20000101
for i in *
do
dname=$(echo $i|awk -F'[-_]' '{print $1_$2}')
mkdir -p $dname
mv "$i" $dname
done