1

I'm looking for a way to paste files with the same names, contained in directories and subdirectories, to another directory with the same subdirectory structure.

For instance :

Dir1/a/a/file1
Dir2/a/a/file1

Those would be pasted into this directory :

Dir3/a/a/file1

Now I found on stackexchange a piece of zsh code that I modified to get the following :

#!/bin/zsh

typeset -A files
for file (dir*/*/*/*(nN)) files[$file:t]+=$file$'\0'
for file (${(k)files}) paste -d "\0" ${(0)files[$file]} > outputDir/*/*/$file

This doesn't work, as the code doesn't understand the "*" after outputDir. If I set a precise set of subdirectories for instance ouputDir/a/a/$file it works like a charm. I don't really know zsh as I discovered it with this piece of code. How could I do to keep the same subdirectories structure for the output dir ?

Thank you for your help. Regards

4
  • 1
    Please edit your question and give us a bit more detail. Does the Dir3/a/a/ already exist? Which of the two original files do you want to keep? You cannot have two files with the same name in the same directory, so you will need to keep one of the two.
    – terdon
    Commented Mar 17, 2023 at 17:03
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Mar 17, 2023 at 18:08
  • Thank you for your answer and sorry for the lack of clarity. I need to perform paste command on files that share the same name in different directories but with the exact same subdirectory structure. I won't keep the two original files afterwards. For instance if file1 on dir1/a/a has one line with "hello" and the file1 on dir2/a/a has one line with "world", i want the newly created file in the (existing but empty) dir3/a/a to have one line containing "helloworld". I would like the code to automatically create the file in the same subdirectory than the two original files. Commented Mar 18, 2023 at 1:07
  • 1
    What do you mean with “paste” in this context? Please be more specific, but don’t answer here; update your question. Commented Mar 18, 2023 at 15:01

1 Answer 1

0

Sounds like you rather want:

#! /bin/zsh -

typeset -A files
for file (dir*/*/*/*(nN)) files[${file#*/}]+=$file$'\0'
for file (${(k)files}) {
  mkdir -p outputDir/$file:h &&
    paste -d '\0' ${(0)files[$file]} > outputDir/$file
}

That is for the keys in that Associative array to be the paths of the files minus the first-level directory component rather than the basename (tail) of the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .