4

How do I paste the content of two folders side by side so that output looks like

some_command dir1 dir2

dir1_file1 dir2_file1
dir1_file2 dir2_file2
dir1_file3 dir2_file3
dir1_file4 dir2_file4
Manfredo
  • 1,760
  • 4
  • 25
  • 53

4 Answers4

3

Have a look in gnu diff utility combined with process substitution:

$ diff -y <(ls ./tmp) <(ls ./tmp2)
20161201.csv                              <
20161202.csv                              <
aa.txt                              aa.txt
a.txt                               a.txt
bb.txt                              bb.txt
bsd.bsd                             bsd.bsd
b.txt                               b.txt

$ diff -y --suppress-common-lines <(ls ./tmp) <(ls ./tmp2)
20161201.csv                              <
20161202.csv                              <

See man diff for more options.

George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
1

You can do it in 3 commands:

ls -1 dir1 > file1
ls -1 dir2 > file2
pr -m -t file1 file2
tale852150
  • 1,618
  • 3
  • 17
  • 23
1

You could use sdiff and view the 2 directories side by side like so:

$ sdiff <(ls -1 dir1) <(ls -1 dir2)

Any differences between the 2 directories will be reflected with a < or > depending on which side of the comparison is missing a specific file/sub-directory.

Example

$ sdiff <(ls -1 dir1) <(ls -1 dir2)
alison_krauss                                                   alison_krauss
Bach - Brandenburg Concertos                                  <
band_of_horses                                                  band_of_horses
Barenaked Ladies                                              <
big_star                                                        big_star
bob_seger                                                       bob_seger
slm
  • 15,396
  • 12
  • 109
  • 124
0

I don't understand the need for the hack of diffing / sdiffing this. This introduces useless processing. The same for pr.

If all you need is to print files from two folders side-by-side, all you need is paste. This command is precisely for not counting, not diffing, but simply printing multiple files side-by-side in (by default) tab-separated fields, one line per file.

Just do:

paste <(ls dir1) <(ls dir2)

You can easily make this into a script or a function.

6 Excellent Free Books to Learn Bash

  1. GNU Bash Reference Manual by Chet Ramey, Brian Fox
  2. Advanced Bash Scripting Guide by Mendel Cooper
  3. The Linux Command Line by William Shotts
  4. Bash Guide for Beginners by Machtelt Garrels
  5. The Bash Guide by Maarten Billemont
  6. Linux Shell Scripting Tutorial by Vivek Gite

#1 is authored by Chet Ramey, the author of bash.

I had at some point a book exclusively dedicated to shell filters, but that was in the early 90s, before bash, Linux, electricity...

EDIT

Here's the function:

function join_by {
  local d=${1-} f=${2-}
  if shift 2; then
    printf %s "$f" "${@/#/$d}"
  fi
}

sbs() {
  d="<(ls `join_by ') <(ls ' $*`)"
  echo $d
  eval paste $d
}

The auxiliary function join_by was contributed here in SO almost a decade ago, because bash, just like diamonds, is forever.

Ricardo
  • 576
  • 8
  • 10