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
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
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.
You can do it in 3 commands:
ls -1 dir1 > file1
ls -1 dir2 > file2
pr -m -t file1 file2
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.
$ 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
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.
#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...
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.