我正在尝试从唯一的文件名创建文件夹。以下是文件:
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