通过自动化将子文件夹及其内容向上移动一级并删除空文件夹,跨多个文件夹

通过自动化将子文件夹及其内容向上移动一级并删除空文件夹,跨多个文件夹

顶部文件夹 (1) 是根文件夹

顶部文件夹 (1) 仅包含文件夹顶部 (2)。

顶层文件夹 (2) 包含顶层文件夹 (3)、(4)、(5)、(6)、(7)、(8)、(9)、(10)

顶部文件夹 (3) 至 (10) 均包含文件夹和文件。

我想做的是将顶级文件夹 (3) 移动到 (10) 上一级,这样父文件夹就是顶级文件夹 (1)。然后使用相同的命令删除现在空的顶级文件夹 (2)

底部文件夹 (1) 显示所需的文件夹结构,包括已删除的文件夹。

奖励:我有一个根文件夹,其中包含数百个这样的文件夹。因此,如果我位于该根文件夹中,我会看到顶部文件夹 (1) 和许多其他文件夹。我理想的解决方案是能够提出一个 bash 行或脚本来遍历每个文件夹并更改文件夹结构,如下所示。

 (1)->Packt.Learn.Javascript.and.HTML5.Canvas.Build.a.Paint.App.for.Drawing/
    (2)->LearnJavascriptandHTML5Canvas-BuildaPaintAppforDrawing[Video]
      (3)->1.Introduction/
      (4)->2.Algorithm of the game app/
      (5)->3.HTML5 Building Blocks of the App/
      (6)->4.Styling the Drawing_Paint app - achieving the final look/
      (7)->5.[OPTIONAL] Javascript Basic Concepts (related to this project)/
      (8)->6.[OPTIONAL]HTML5 Canvas Basic Concepts(related to this project)/
      (9)->7.Javascript code for the app - Making the app work/
     (10)->Exercise Files/

 (1)->Packt.Learn.Javascript.and.HTML5.Canvas.Build.a.Paint.App.for.Drawing/
   (2)->1.Introduction/
   (3)->2.Algorithm of the game app/
   (4)->3.HTML5 Building Blocks of the App/
   (5)->4.Styling the Drawing_Paint app - achieving the final look/
   (6)->5.[OPTIONAL] Javascript Basic Concepts (related to this project)/
   (7)->6.[OPTIONAL] HTML5 Canvas Basic Concepts (related to this project)/
   (8)->7.Javascript code for the app - Making the app work/
   (9)->Exercise Files/

答案1

你有一个bash标签,所以这里是一个使用 bash 数组的解决方案:

for book in *; do topdir=($book/*); chapters=("${topdir[0]}"/*); mv "${chapters[@]}" "$book"; rmdir "${topdir[0]}"; done

您需要从您提到的根文件夹运行此命令,以便“book”变量解析为每本书的名称。

该解决方案以这种方式工作。对于每本书,使用数组变量“topdir”读取顶级目录名称。从那里获取顶级目录下的章节目录列表。然后,将章节目录列表传递给 ,mv以便将其向上移动一级。完成后,使用 删除现在空的顶级目录rmdirrmdir将拒绝删除不为空的目录,因此它是一个安全网。

这是一个演示,显示了运行该命令之前和之后的目录结构(我在此系统上没有该tree命令,因此是 的输出find):

前:

$ find .
.
./Learn.Javascript.and.HTML5
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/1.Introduction
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/2.Algorithm of the game app
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/3.HTML5 Building Blocks of the App
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/4.Styling the Drawing_Paint app - achieving the final look
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/5.[OPTIONAL] Javascript Basic Concepts (related to this project)
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/6.[OPTIONAL]HTML5 Canvas Basic Concepts(related to this project)
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/7.Javascript code for the app - Making the app work
./Learn.Javascript.and.HTML5/LearnJavascriptandHTML5Canvas[Video]/Exercise Files

后:

$ find .
.
./Learn.Javascript.and.HTML5
./Learn.Javascript.and.HTML5/1.Introduction
./Learn.Javascript.and.HTML5/2.Algorithm of the game app
./Learn.Javascript.and.HTML5/3.HTML5 Building Blocks of the App
./Learn.Javascript.and.HTML5/4.Styling the Drawing_Paint app - achieving the final look
./Learn.Javascript.and.HTML5/5.[OPTIONAL] Javascript Basic Concepts (related to this project)
./Learn.Javascript.and.HTML5/6.[OPTIONAL]HTML5 Canvas Basic Concepts(related to this project)
./Learn.Javascript.and.HTML5/7.Javascript code for the app - Making the app work
./Learn.Javascript.and.HTML5/Exercise Files

相关内容