使用终端将文件移动到新文件夹结构

时间:2017-07-06 04:27:51

标签: bash shell

简单问题:我需要使用终端中的一些shell脚本将图像从旧目录结构移动到新目录结构。旧文件结构如下:

Parent.Folder -- Folder1
                 Folder2
                 Folder3

在每个FolderX中,我们有更多文件夹:

Folder1 -- CAM1
           CAM2
           CAM3

在每个CAM文件夹中,我们都有图像:

CAM1 -- image1.jpg
        image2.jpg
        image3.jpg

我需要切换文件夹结构,以便:

Parent.Folder -- CAM1
                 CAM2
                 CAM3

含:

CAM1 -- Folder1 -- image1.jpg
                -- image2.jpg
                -- image3.jpg
     -- Folder2 -- ...etc...
     -- Folder3 -- ...etc...

脚本需要是动态的,因为我需要将它应用于许多不同大小的数据集。

感谢任何帮助。提前谢谢!

1 个答案:

答案 0 :(得分:1)

$ cd parent.folder
$ find . -type f
./Folder3/CAM2/image3.jpg
./Folder3/CAM2/image2.jpg
./Folder3/CAM2/image1.jpg
./Folder3/CAM1/image3.jpg
./Folder3/CAM1/image2.jpg
./Folder3/CAM1/image1.jpg
./Folder2/CAM3/image3.jpg
./Folder2/CAM3/image2.jpg
./Folder2/CAM3/image1.jpg
./Folder2/CAM2/image3.jpg
./Folder2/CAM2/image2.jpg
./Folder2/CAM2/image1.jpg
./Folder2/CAM1/image3.jpg
./Folder2/CAM1/image2.jpg
./Folder2/CAM1/image1.jpg
./Folder1/CAM3/image3.jpg
./Folder1/CAM3/image2.jpg
./Folder1/CAM3/image1.jpg
./Folder1/CAM2/image3.jpg
./Folder1/CAM2/image2.jpg
./Folder1/CAM2/image1.jpg
./Folder1/CAM1/image3.jpg
./Folder1/CAM1/image2.jpg
./Folder1/CAM1/image1.jpg

$ for i in Folder*/CAM*/; do
    mkdir -p "$(basename $i)";
    mv "$i" "$(basename "$i")/$(dirname "$i")";
  done

 $ find . -type f
./CAM3/Folder2/image3.jpg
./CAM3/Folder2/image2.jpg
./CAM3/Folder2/image1.jpg
./CAM3/Folder1/image3.jpg
./CAM3/Folder1/image2.jpg
./CAM3/Folder1/image1.jpg
./CAM2/Folder3/image3.jpg
./CAM2/Folder3/image2.jpg
./CAM2/Folder3/image1.jpg
./CAM2/Folder2/image3.jpg
./CAM2/Folder2/image2.jpg
./CAM2/Folder2/image1.jpg
./CAM2/Folder1/image3.jpg
./CAM2/Folder1/image2.jpg
./CAM2/Folder1/image1.jpg
./CAM1/Folder3/image3.jpg
./CAM1/Folder3/image2.jpg
./CAM1/Folder3/image1.jpg
./CAM1/Folder2/image3.jpg
./CAM1/Folder2/image2.jpg
./CAM1/Folder2/image1.jpg
./CAM1/Folder1/image3.jpg
./CAM1/Folder1/image2.jpg
./CAM1/Folder1/image1.jpg

请注意,此代码假定您提到的某些文件夹结构。如果你在问题中提到的与现实有任何不同,那么这可能不起作用。