0

In a project I am working on, I originally had some directory named assets, with a large number of directories, whose names are not predictable, each of these directories contained a number of files, whose names were the same for each directory (but with different content). My original file-structure would look something like this:

assets
|
|-some_dir
|  |-blue.txt
|  |-apple.txt
|  |-red.txt
|  |-banana.txt
|
|-some_other_dir
|  |-blue.txt
|  |-apple.txt
|  |-red.txt
|  |-banana.txt
...
|-yet_another_dir
|  |-blue.txt
|  |-apple.txt
|  |-red.txt
|  |-banana.txt

Obviously, the files and directories don't have these exact names. I want to introduce a new file structure, such that the files stay in the same directories, but are put within other directories within these directories: ie. I want:

assets
|
|-some_dir
|  |-colors
|  |  |-blue.txt
|  |  |-red.txt
|  |-fruits
|  |  |-apple.txt
|  |  |-banana.txt
|
|-some_other_dir
|  |-colors
|  |  |-blue.txt
|  |  |-red.txt
|  |-fruits
|  |  |-apple.txt
|  |  |-banana.txt
...
|-yet_another_dir
|  |-colors
|  |  |-blue.txt
|  |  |-red.txt
|  |-fruits
|  |  |-apple.txt
|  |  |-banana.txt

There are so many directories in assets, that going through each by hand is not an option, so I want to do this in one, or at least very few shell-commands (I am on zsh by the way).

I am able to create the subdirectories inside each directory, using the reply to this question but I can not figure out how to move the files into the new directory, keeping in mind that each file shall only be moved to the directory inside the directory it is in already.

Edit: since this is not clear: I am willing to move each of the files (per name) individually to sort them correctly (there are only a handful of files per directory) but I will not repeat this for each individual directory

4
  • How would your new tool know that banana is a fruit and blue is a colour? – RudiC Dec 31 '19 at 14:33
  • I would be willing to move each of the files (per name) individually to sort them correctly (there are only a handful) but I will not repeat this for each individual directory – Nikolaj Dec 31 '19 at 14:50
  • So you have a "template" directory with all files set / distributed correctly, and you want that repeated for all further directories? How would your new tool know the template directory? – RudiC Dec 31 '19 at 14:55
  • If you by "template" mean that I know what the final result for each directory shall look like, yes; but what I need is just an automated way of repeating mv file folder (for instance mv blue.txt colors/) inside every single directory; and I am assuming that such an automated way already exists, so I did not think that I was asking for a new tool. – Nikolaj Dec 31 '19 at 15:10
1

Something like:

autoload -U zmv
mkdir-and-mv() {
  mkdir -p -- "$2:h" &&
    mv -- "$@"
}

typeset -A c
c=(
  blue   colors
  red    colors
  apple  fruits
  banana fruits
)

zmv -n -P mkdir-and-mv "(**/)((${(j:|:)${(@bk)c}}).txt)" '$1$c[$3]/$2'

(remove the -n for dry-run if happy)

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.