I am trying to concat all files with similar names but in different subdirectories.

I found this solution for exact file matches but I am not sure how to adapt it for partial matches

Create new concatenated files of same name in multiple directories

example file paths:



I have 20 different ch directories, and the final number in the file name matches the directory number.

I have a file with the names I am trying to concat.


How can I adapt this solution or concat this files in another way? End result I would like one directory with one file for each name in my file list (in this example the end result would be 3 files called name1, name2 and name3). we

  • It's not clear what you're trying to achieve. So you have different files, do you want them to be merged by mask (e.g. all name1.*.txt go to name1 regardless of directory) or by directory (e.g. all files in ch1 go to name1) or by some other way (e.g. all *1.assoc.txt go to name1)? Can you please add this information to your question?
    – rush
    Dec 18, 2021 at 0:00
  • @rush I would like all name1.*.assoc.txt files to go to name1 regardless of directory. I edited the question for clarity
    – keenan
    Dec 18, 2021 at 17:10

1 Answer 1


In zsh:

for name (${(f)"$(<names.txt)"})
  cat -- out/ch<->/$name.<->.assoc.txt(.n) > outdir/$name

In bash:

zsh -c '
  for name (${(f)"$(<names.txt)"})
    cat -- out/ch<->/$name.<->.assoc.txt(.n) > outdir/$name

If you can't install zsh, you can do something approaching in bash and on a GNU system with:

readarray -t files < <(grep . names.txt)
shopt -s extglob failglob
for name in "${files[@]}"; do
  printf '%s\0' out/ch+([0123456789])/"$name".+([0123456789]).assoc.txt |
    sort -zV |
    xargs -r0 cat -- > outdir/"$name"
  • I am using bash not zsh and I get the following bash: syntax error near unexpected token '(' on the first line
    – keenan
    Dec 20, 2021 at 18:09
  • @keeman, yes zsh and bash are interpreters for two different languages. In bash, you'd run zsh -c 'the code as shown' Dec 20, 2021 at 18:15
  • that results in the error bash: zsh: command not found
    – keenan
    Dec 20, 2021 at 18:19
  • This works but it also copies all of the old files into outdir
    – keenan
    Dec 22, 2021 at 16:55
  • @keenan, what old files? Dec 22, 2021 at 17:25

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.