Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a number of files with this pattern:


and so on.

I need to "pair them" and put them in a file like this

file1_foo.txt    file1_bar.txt

(tab separated)

What's the best course of action in this case?

share|improve this question
up vote 4 down vote accepted

I assume you don't want to just assume both exist. This will only print the lines if they both exist.

for file1 in *_foo.ext; do
    if [[ -e "$file2" ]]; then
        printf '%s\t%s\n' "$file1" "$file2"


$ tee {1,2,3}_foo.ext {1,3,4}_bar.ext </dev/null >/dev/null
$ ./script
1_foo.ext   1_bar.ext
3_foo.ext   3_bar.ext
share|improve this answer
Right, I forgot about % and extensions. I'll test this at once. – Einar Apr 3 '13 at 8:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.