I had imagined the simplest way to compare the contents of two similar directories would be something like
diff `ls old` `ls new`
But I see why this doesn't work; diff
is being handed a big long list of files on the command line, rather than two streams like I had hoped. How do I pass the two outputs to diff directly?
Best Answer
Command substitution
`…`
substitutes the output of the command into the command line, sodiff
sees the list of files in both directories as arguments. What you want is fordiff
to see two file names on its command line, and have the contents of these files be the directory listings. That's what process substitution does.The arguments to
diff
will look like/dev/fd/3
and/dev/fd/4
: they are file descriptors corresponding to two pipes created by bash. Whendiff
opens these files, it'll be connected to the read side of each pipe. The write side of each pipe is connected to thels
command.