Flattening directory hierarchy preserving directory names in new directory name












3














I basically want to go from this:



.
├── Alan Walker
│   ├── Different World
│   │ ├── 01 Intro.mp3
│   │ ├── 02 Lost Control.mp3
│   │ └── cover.jpg
│   └── Same World
│   ├── 01 Intro.mp3
│   └── 02 Found Control.mp3
├── Aurora
│   └── Infections Of A Different Kind Step 1
│   ├── 01 Queendom.lrc
│   ├── 02 Forgotten Love.lrc
│   └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses
    └── Use Your Illusion I
   ├── 01 Right Next Door To Hell.lrc
   ├── 01 Right Next Door To Hell.mp3
   ├── 02 Dust N' Bones.lrc
   └── 02 Dust N' Bones.mp3


to this:



.
├── Alan Walker - Different World
│ ├── 01 Intro.mp3
│ ├── 02 Lost Control.mp3
│ └── cover.jpg
├── Alan Walker - Same World
│ ├── 01 Intro.mp3
│ └── 02 Found Control.mp3
├── Aurora - Infections Of A Different Kind Step 1
│ ├── 01 Queendom.lrc
│ ├── 02 Forgotten Love.lrc
│ └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses - Use Your Illusion I
├── 01 Right Next Door To Hell.lrc
├── 01 Right Next Door To Hell.mp3
├── 02 Dust N' Bones.lrc
└── 02 Dust N' Bones.mp3


None of the existing solutions I could find included renaming the directory itself. It'd be great to be able to do this with zmv, but I can't figure out how to.










share|improve this question
























  • Not a full solution, so not an answer: easytag may let you do this, but only for audio files - I don't think it will move covers, etc.
    – rvs
    57 mins ago






  • 2




    Could there be more than one album for a specific artist within one artist directory? Is the structure always three levels deep?
    – nohillside
    39 mins ago






  • 1




    @nohillside Yes, There can be more than one album for an artist. And yes, the structure is always three levels deep.
    – aksh1618
    37 mins ago
















3














I basically want to go from this:



.
├── Alan Walker
│   ├── Different World
│   │ ├── 01 Intro.mp3
│   │ ├── 02 Lost Control.mp3
│   │ └── cover.jpg
│   └── Same World
│   ├── 01 Intro.mp3
│   └── 02 Found Control.mp3
├── Aurora
│   └── Infections Of A Different Kind Step 1
│   ├── 01 Queendom.lrc
│   ├── 02 Forgotten Love.lrc
│   └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses
    └── Use Your Illusion I
   ├── 01 Right Next Door To Hell.lrc
   ├── 01 Right Next Door To Hell.mp3
   ├── 02 Dust N' Bones.lrc
   └── 02 Dust N' Bones.mp3


to this:



.
├── Alan Walker - Different World
│ ├── 01 Intro.mp3
│ ├── 02 Lost Control.mp3
│ └── cover.jpg
├── Alan Walker - Same World
│ ├── 01 Intro.mp3
│ └── 02 Found Control.mp3
├── Aurora - Infections Of A Different Kind Step 1
│ ├── 01 Queendom.lrc
│ ├── 02 Forgotten Love.lrc
│ └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses - Use Your Illusion I
├── 01 Right Next Door To Hell.lrc
├── 01 Right Next Door To Hell.mp3
├── 02 Dust N' Bones.lrc
└── 02 Dust N' Bones.mp3


None of the existing solutions I could find included renaming the directory itself. It'd be great to be able to do this with zmv, but I can't figure out how to.










share|improve this question
























  • Not a full solution, so not an answer: easytag may let you do this, but only for audio files - I don't think it will move covers, etc.
    – rvs
    57 mins ago






  • 2




    Could there be more than one album for a specific artist within one artist directory? Is the structure always three levels deep?
    – nohillside
    39 mins ago






  • 1




    @nohillside Yes, There can be more than one album for an artist. And yes, the structure is always three levels deep.
    – aksh1618
    37 mins ago














3












3








3







I basically want to go from this:



.
├── Alan Walker
│   ├── Different World
│   │ ├── 01 Intro.mp3
│   │ ├── 02 Lost Control.mp3
│   │ └── cover.jpg
│   └── Same World
│   ├── 01 Intro.mp3
│   └── 02 Found Control.mp3
├── Aurora
│   └── Infections Of A Different Kind Step 1
│   ├── 01 Queendom.lrc
│   ├── 02 Forgotten Love.lrc
│   └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses
    └── Use Your Illusion I
   ├── 01 Right Next Door To Hell.lrc
   ├── 01 Right Next Door To Hell.mp3
   ├── 02 Dust N' Bones.lrc
   └── 02 Dust N' Bones.mp3


to this:



.
├── Alan Walker - Different World
│ ├── 01 Intro.mp3
│ ├── 02 Lost Control.mp3
│ └── cover.jpg
├── Alan Walker - Same World
│ ├── 01 Intro.mp3
│ └── 02 Found Control.mp3
├── Aurora - Infections Of A Different Kind Step 1
│ ├── 01 Queendom.lrc
│ ├── 02 Forgotten Love.lrc
│ └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses - Use Your Illusion I
├── 01 Right Next Door To Hell.lrc
├── 01 Right Next Door To Hell.mp3
├── 02 Dust N' Bones.lrc
└── 02 Dust N' Bones.mp3


None of the existing solutions I could find included renaming the directory itself. It'd be great to be able to do this with zmv, but I can't figure out how to.










share|improve this question















I basically want to go from this:



.
├── Alan Walker
│   ├── Different World
│   │ ├── 01 Intro.mp3
│   │ ├── 02 Lost Control.mp3
│   │ └── cover.jpg
│   └── Same World
│   ├── 01 Intro.mp3
│   └── 02 Found Control.mp3
├── Aurora
│   └── Infections Of A Different Kind Step 1
│   ├── 01 Queendom.lrc
│   ├── 02 Forgotten Love.lrc
│   └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses
    └── Use Your Illusion I
   ├── 01 Right Next Door To Hell.lrc
   ├── 01 Right Next Door To Hell.mp3
   ├── 02 Dust N' Bones.lrc
   └── 02 Dust N' Bones.mp3


to this:



.
├── Alan Walker - Different World
│ ├── 01 Intro.mp3
│ ├── 02 Lost Control.mp3
│ └── cover.jpg
├── Alan Walker - Same World
│ ├── 01 Intro.mp3
│ └── 02 Found Control.mp3
├── Aurora - Infections Of A Different Kind Step 1
│ ├── 01 Queendom.lrc
│ ├── 02 Forgotten Love.lrc
│ └── 03 Gentle Earthquakes.mp3
└── Guns N' Roses - Use Your Illusion I
├── 01 Right Next Door To Hell.lrc
├── 01 Right Next Door To Hell.mp3
├── 02 Dust N' Bones.lrc
└── 02 Dust N' Bones.mp3


None of the existing solutions I could find included renaming the directory itself. It'd be great to be able to do this with zmv, but I can't figure out how to.







bash zsh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 34 mins ago

























asked 1 hour ago









aksh1618

256




256












  • Not a full solution, so not an answer: easytag may let you do this, but only for audio files - I don't think it will move covers, etc.
    – rvs
    57 mins ago






  • 2




    Could there be more than one album for a specific artist within one artist directory? Is the structure always three levels deep?
    – nohillside
    39 mins ago






  • 1




    @nohillside Yes, There can be more than one album for an artist. And yes, the structure is always three levels deep.
    – aksh1618
    37 mins ago


















  • Not a full solution, so not an answer: easytag may let you do this, but only for audio files - I don't think it will move covers, etc.
    – rvs
    57 mins ago






  • 2




    Could there be more than one album for a specific artist within one artist directory? Is the structure always three levels deep?
    – nohillside
    39 mins ago






  • 1




    @nohillside Yes, There can be more than one album for an artist. And yes, the structure is always three levels deep.
    – aksh1618
    37 mins ago
















Not a full solution, so not an answer: easytag may let you do this, but only for audio files - I don't think it will move covers, etc.
– rvs
57 mins ago




Not a full solution, so not an answer: easytag may let you do this, but only for audio files - I don't think it will move covers, etc.
– rvs
57 mins ago




2




2




Could there be more than one album for a specific artist within one artist directory? Is the structure always three levels deep?
– nohillside
39 mins ago




Could there be more than one album for a specific artist within one artist directory? Is the structure always three levels deep?
– nohillside
39 mins ago




1




1




@nohillside Yes, There can be more than one album for an artist. And yes, the structure is always three levels deep.
– aksh1618
37 mins ago




@nohillside Yes, There can be more than one album for an artist. And yes, the structure is always three levels deep.
– aksh1618
37 mins ago










1 Answer
1






active

oldest

votes


















4














Something like this maybe?



for topdir in */; do
topdir_name=$( basename "$topdir" )

for subdir in "$topdir"/*/; do
subdir_name=$( basename "$subdir" )

newdir="$topdir_name - $subdir_name"
mkdir "$newdir" || continue

mv "$subdir"/* "$newdir"
rmdir "$subdir"
done

rmdir "$topdir"
done


This goes through all the top-level directories in the current directory (the band names). For each such directory, it goes through its subdirectories (the album names). For each pair of band name and album name, a new directory is created and the files from the subdirectory are moved to it. The album subdirectories are removed when they have been processed, as are the original band top-level directories.



The rmdir calls will fail if any directory contains hidden filenames or if any of the new directories failed to be created.



This is totally untested code. Run it on a backed-up copy of your files.






share|improve this answer























  • I mean, yes, something like would work, but I'm kinda looking for a one or two line solution, to run easily from terminal. Some find/sed/zmv magic :)
    – aksh1618
    25 mins ago










  • Put ot into a script, and -boom- it becomes a one-liner.
    – Hans-Martin Mosner
    8 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492030%2fflattening-directory-hierarchy-preserving-directory-names-in-new-directory-name%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Something like this maybe?



for topdir in */; do
topdir_name=$( basename "$topdir" )

for subdir in "$topdir"/*/; do
subdir_name=$( basename "$subdir" )

newdir="$topdir_name - $subdir_name"
mkdir "$newdir" || continue

mv "$subdir"/* "$newdir"
rmdir "$subdir"
done

rmdir "$topdir"
done


This goes through all the top-level directories in the current directory (the band names). For each such directory, it goes through its subdirectories (the album names). For each pair of band name and album name, a new directory is created and the files from the subdirectory are moved to it. The album subdirectories are removed when they have been processed, as are the original band top-level directories.



The rmdir calls will fail if any directory contains hidden filenames or if any of the new directories failed to be created.



This is totally untested code. Run it on a backed-up copy of your files.






share|improve this answer























  • I mean, yes, something like would work, but I'm kinda looking for a one or two line solution, to run easily from terminal. Some find/sed/zmv magic :)
    – aksh1618
    25 mins ago










  • Put ot into a script, and -boom- it becomes a one-liner.
    – Hans-Martin Mosner
    8 mins ago
















4














Something like this maybe?



for topdir in */; do
topdir_name=$( basename "$topdir" )

for subdir in "$topdir"/*/; do
subdir_name=$( basename "$subdir" )

newdir="$topdir_name - $subdir_name"
mkdir "$newdir" || continue

mv "$subdir"/* "$newdir"
rmdir "$subdir"
done

rmdir "$topdir"
done


This goes through all the top-level directories in the current directory (the band names). For each such directory, it goes through its subdirectories (the album names). For each pair of band name and album name, a new directory is created and the files from the subdirectory are moved to it. The album subdirectories are removed when they have been processed, as are the original band top-level directories.



The rmdir calls will fail if any directory contains hidden filenames or if any of the new directories failed to be created.



This is totally untested code. Run it on a backed-up copy of your files.






share|improve this answer























  • I mean, yes, something like would work, but I'm kinda looking for a one or two line solution, to run easily from terminal. Some find/sed/zmv magic :)
    – aksh1618
    25 mins ago










  • Put ot into a script, and -boom- it becomes a one-liner.
    – Hans-Martin Mosner
    8 mins ago














4












4








4






Something like this maybe?



for topdir in */; do
topdir_name=$( basename "$topdir" )

for subdir in "$topdir"/*/; do
subdir_name=$( basename "$subdir" )

newdir="$topdir_name - $subdir_name"
mkdir "$newdir" || continue

mv "$subdir"/* "$newdir"
rmdir "$subdir"
done

rmdir "$topdir"
done


This goes through all the top-level directories in the current directory (the band names). For each such directory, it goes through its subdirectories (the album names). For each pair of band name and album name, a new directory is created and the files from the subdirectory are moved to it. The album subdirectories are removed when they have been processed, as are the original band top-level directories.



The rmdir calls will fail if any directory contains hidden filenames or if any of the new directories failed to be created.



This is totally untested code. Run it on a backed-up copy of your files.






share|improve this answer














Something like this maybe?



for topdir in */; do
topdir_name=$( basename "$topdir" )

for subdir in "$topdir"/*/; do
subdir_name=$( basename "$subdir" )

newdir="$topdir_name - $subdir_name"
mkdir "$newdir" || continue

mv "$subdir"/* "$newdir"
rmdir "$subdir"
done

rmdir "$topdir"
done


This goes through all the top-level directories in the current directory (the band names). For each such directory, it goes through its subdirectories (the album names). For each pair of band name and album name, a new directory is created and the files from the subdirectory are moved to it. The album subdirectories are removed when they have been processed, as are the original band top-level directories.



The rmdir calls will fail if any directory contains hidden filenames or if any of the new directories failed to be created.



This is totally untested code. Run it on a backed-up copy of your files.







share|improve this answer














share|improve this answer



share|improve this answer








edited 26 mins ago

























answered 31 mins ago









Kusalananda

122k16229373




122k16229373












  • I mean, yes, something like would work, but I'm kinda looking for a one or two line solution, to run easily from terminal. Some find/sed/zmv magic :)
    – aksh1618
    25 mins ago










  • Put ot into a script, and -boom- it becomes a one-liner.
    – Hans-Martin Mosner
    8 mins ago


















  • I mean, yes, something like would work, but I'm kinda looking for a one or two line solution, to run easily from terminal. Some find/sed/zmv magic :)
    – aksh1618
    25 mins ago










  • Put ot into a script, and -boom- it becomes a one-liner.
    – Hans-Martin Mosner
    8 mins ago
















I mean, yes, something like would work, but I'm kinda looking for a one or two line solution, to run easily from terminal. Some find/sed/zmv magic :)
– aksh1618
25 mins ago




I mean, yes, something like would work, but I'm kinda looking for a one or two line solution, to run easily from terminal. Some find/sed/zmv magic :)
– aksh1618
25 mins ago












Put ot into a script, and -boom- it becomes a one-liner.
– Hans-Martin Mosner
8 mins ago




Put ot into a script, and -boom- it becomes a one-liner.
– Hans-Martin Mosner
8 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492030%2fflattening-directory-hierarchy-preserving-directory-names-in-new-directory-name%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Understanding the information contained in the Deep Space Network XML data?

Ross-on-Wye

Eastern Orthodox Church