bash diff - ghdrako/doc_snipets GitHub Wiki

diff -y file1 file2  # show difference side by side

diff -u print_in_js.js  print_in_js_Fixed.js > patched_print_js.diff  # -u uniform output using in patch command
patch print_in_js.js  < patched_print_js.diff

diff <(command1) <(command2)

diff <(ls -l | sort) <(ls -al | sort)

Exit Codes

  • 0: Files are identical.
  • 1: Files are different.
  • 2: Error (inaccessible files, etc.) Example: File Changes
diff file1.txt file2.txt
if [ $? -eq 0 ]; then
  echo "Files have not changed."
elif [ $? -eq 1 ]; then
  echo "Files are different. Update needed."
else
  echo "Error comparing files."
fi