Jun Cheng
Jun Cheng

Reputation: 171

Linux: concatenate each pair of files in a directory

Under linux, in a directory I have multiple files, say n, denoted as file1, file2 ... filen.

I want to concatenate each pair of them. Basically, there are "n choose 2" possible pairs.

like:

file1_file2

file1_file3

...

file1_filen

file2_file3

file2_file4

...

file2_filen

...

I want to use linux command, like cat. could anyone tell me how to use a loop to do it?

Upvotes: 0

Views: 268

Answers (2)

FractalSpace
FractalSpace

Reputation: 5685

Here you go (rev 2):

#!/bin/bash
for i in `seq 1 10`; do
    for j in `seq $((i+1)) 10`; do
        cat file$i file$j >file${i}_file${j}
    done
done

Upvotes: 1

Raul Andres
Raul Andres

Reputation: 3806

Try simply

 for i in file*; do
   for j in file*; do
      cat $i $j >> result_folder/$i_$j;
      rm result_folder/$j_$i 2>/dev/null
   done
 done

Upvotes: 1

Related Questions