I have a list of files (see below) I need to merge into separate files (merge1, merge2). merge1 would contain file1a_1 and file1a_2; merge2 would contain file2a_1, file2a_2.

I tried

find . -name "file*_*"  -exec cat {} \; >> mergefile*

i.e. Files:


The shell script puts all the files into merge file and doesn't separate them out individually.

Any assistance would be appreciated

  • Are the file*_* files in subdirectories, or all in the current directory? I'm assuming the X1a_2 is meant as an example of a file to omit?
    – Jeff Schaller
    Mar 30, 2018 at 0:52

2 Answers 2


If the files are all in the current directory, you could hard-code a loop through the highest number that you might merge (100 in the example below):

shopt -s nullglob
  inputs=( "file${index}"*_* )
  if [ "${#inputs[@]}" -gt 0 ]
    cat -- "${inputs[@]}" > "merge${index}"

Maybe something like:

find . -name 'file[0-9]*_*' -type f -exec awk '
  FNR == 1 {
    output = FILENAME
    sub(/.*\/file/, "", output)
    sub(/[^0-9].*/, "", output)
    output = "merge" output
  {print > output}' {} +

Note that find finds files in an unspecified order, so files will be merged in an unspecified order.

Note that if all the files for a given merge file are empty, the merge file won't be created. If you'd rather get an empty merge file in those cases, and if your awk is the GNU ones, you can change the above to:

find . -name 'file[0-9]*_*' -type f -exec gawk '
    output = FILENAME
    sub(/.*\/file/, "", output)
    sub(/[^0-9].*/, "", output)
    output = "merge" output
    printf "" > output
  {print > output}' {} +
  • your code not work on my debian 9.3
    – Baba
    Mar 30, 2018 at 13:36
  • and too this code not work!! find . -name 'file[0-9]*_*' -type f -exec awk '{print FILENAME}' {} + why?!
    – Baba
    Mar 30, 2018 at 13:37
  • this command find . -name 'file[0-9]*_*' -type f work and show file name ..
    – Baba
    Mar 30, 2018 at 13:38
  • @بارپابابا, in which way does it not work? Do you get an error? What's your shell? If using fish or rc, you'd need to quote {}. Mar 30, 2018 at 13:49
  • i use bash in default! bayanbox.ir/view/4393624869319520231/…
    – Baba
    Mar 30, 2018 at 14:30

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.