Edited version to a rushed question!
When I run diff on two directories ex: $ diff Linux1 Linux2
The output being $ Only in Linux2: COMPLETELYDIFFERENT22.txt
I want to the copy that file(not just the output) into a new directory, created in the same command. I have seen this question many times, but none of the answers work! I'm sure the answer is frustratingly simple, but I just cant seem to find it.
If I understand you correctly, you want to compare files in two directories and copy the files with different contents to a third directory. I don't see a way to do that with just one command (unless you write your own script or function). But one of the strong points of the bash shell is that you can chain commands, using the output of one command as the input of the next.
First, you can use diff as usual:
That will give you an output along the lines of
Now, you need to extract the file paths from that output. You could use a number of commands to do that,
cut
,grep
,sed
,awk
or others. I'll useawk
in this example and extract the files fromdir2
:The output of this pipeline would be something like
That output can now be fed to
xargs
as follows:Please note that this method only works when the differing files exist in both directories. If there's a file that only exists in one of the two directories,
diff
s output will be different, which will make cutting the filename out fail. Also, the file names must not contain any spaces, because that would trip up the cutting as well.