Saturday, May 4, 2024
424
rated 0 times [  424] [ 0]  / answers: 1 / hits: 138284  / 1 Year ago, wed, march 8, 2023, 6:45:39

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?


More From » command-line

 Answers
5

Command substitution `…` substitutes the output of the command into the command line, so diff sees the list of files in both directories as arguments. What you want is for diff 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.



diff <(ls old) <(ls new)


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. When diff opens these files, it'll be connected to the read side of each pipe. The write side of each pipe is connected to the ls command.


[#33654] Thursday, March 9, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lasagnerhead

Total Points: 487
Total Questions: 105
Total Answers: 122

Location: French Southern and Antarctic Lands
Member since Sun, May 3, 2020
4 Years ago
;