How to extract a specifically named folder from a recursive directory, delete the others

directorydirectory-structuremvrecursive

I have this kind of directory tree that is obtained from unzipping a zip file:

x -> y -> z -> run -> FILES AND DIRECTORIES HERE

So there are 4 directories, 3 of whom are empty of files (x, y, z) and only contain 1 sub-directory, and there is the directory I am interested in, named "run".

I want to move the "run" directory itself (including everything within) to the "root" location where I unzipped (i.e. where "x" is, but not inside "x").

Assumptions: There exists a folder named "run", but I do not know how many directories I will have to "cd" to get to it (could be 3 (x,y,z), could be 10 or more. The names are also unknown and do not have to be x,y,z etc).

How can I accomplish this? I tried many variations of this question but they all failed.

Best Answer

what about

  find . -type d -name run -exec mv {} /path/to/X \;

where

  • /path/to/X is your destination directory
  • you start from this same place.
  • then use other answer to remove empty directories.

(on a side note there is a --junk-paths option in zip, either when zipping or unzipping)

Related Question