Bash: Use exec find option by sorting two file arguments

bashfindsort

Let's say I want to compare the files with the same filename between these two directories:

 /tmp/datadir/dir1/dir2/

and

 /datadir/dir1/dir2/

It is required to sort them before comparing.

I am currently in the /tmp directory, and I tried to run this find with the exec option:

find datadir -type f -exec sdiff -s <( sort {} ) <( sort "/"{} ) \;

But, it gives me an error

sort: open failed: {}: No such file or directory

sort: open failed: /{}: No such file or directory

However, the below command works fine, but the data is not sorted for proper comparison.

find data -type f -exec sdiff -s  {}  "/"{} \;

How can I fix this problem?

Best Answer

The replacement of {} is done by find and happens after the evaluation of the process substitution (<(…)) and such in your code.

Using bash -c:

find datadir -type f -exec \
  bash -c 'sdiff -s <( sort "$1" ) <( sort "/$1" )' bash {} \;

Or to avoid running one bash instance per file:

find datadir -type f -exec bash -c '
  for file do
    sdiff -s <( sort "$file" ) <( sort "/$file" )
  done' bash {} +
Related Question