2

Top Folder (1) is the root folder

Top Folder (1) contains only the folder Top (2).

Top Folder (2) contains Top folders (3),(4),(5),(6),(7),(8),(9),(10)

Top Folder (3) to (10) all containing folders and files.

What I'm trying to do is to move Top Folders (3) to (10) up one level so there parent folder is Top Folder (1). Then with the same command remove the now empty Top Folder (2)

Bottum Folder (1) shows the desired folder structure including the removed folder.

BONUS: I have a root folder that contains hundreds of these folders. So if I was in that root folder I would see Top Folder (1) and many others. My ideal solution would be to be able to come up with a bash line or script that would traverse into every folder and change the folder structure like shown below.

 (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/
  • I've tried some ways with using Find and XARGS but anything I've tried so far fails. – Greg Bates Mar 29 at 2:19
1

You have a bash tag, so here is a solution that uses bash's arrays:

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

You'll need to run this from root folder that you have mentioned, so that the 'book' variable resolves to the name of each book.

This solution works in this manner. For each book, read the top directory name using the array variable, 'topdir'. From there, get the list of chapter directories under the top-level directory. Then, pass the list of chapter directories to mv so that it can be moved one level up. Once that is done, remove the now-empty top-level directory using rmdir. rmdir will refuse to remove directories that are not empty, so it's something of a safety net.

Here's a demo showing the directory structure before and after running the command (I did not have the tree command on this system, hence the output from find):

Before:

$ 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

After:

$ 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.