Skip to content
shellmap

diffCompare two files or directories line-by-line and report the differences across all 5 shells

Equivalents in every shell

Bashunix
diff file1.txt file2.txt
Zshunix
diff file1.txt file2.txt
Fishunix
diff file1.txt file2.txt
PowerShellwindows
Compare-Object (Get-Content file1.txt) (Get-Content file2.txt)

Emits diff *objects* (`InputObject` + `SideIndicator` `<=` / `=>`), not Unix unified-diff text. For patch-compatible output, install Git Bash / WSL diff.

cmd.exewindows
fc file1.txt file2.txt

`fc` (file compare) prints differing line ranges, not unified-diff hunks. Use `fc /n` to include line numbers and `fc /b` for binary mode.

Worked examples

Show a unified (patch-ready) diff between two files

Bash
diff -u file1.txt file2.txt
Fish
diff -u file1.txt file2.txt
PowerShell
git diff --no-index file1.txt file2.txt
cmd.exe
git diff --no-index file1.txt file2.txt

Recursively diff two directory trees

Bash
diff -r dir1/ dir2/
PowerShell
Compare-Object (Get-ChildItem -Recurse dir1) (Get-ChildItem -Recurse dir2) -Property FullName
cmd.exe
fc /l /n dir1\*.* dir2\*.*

Ignore whitespace-only changes

Bash
diff -w file1.txt file2.txt
PowerShell
Compare-Object (Get-Content file1.txt | %{$_.Trim()}) (Get-Content file2.txt | %{$_.Trim()})

Gotchas

  • `diff` exit code is 0 when files are identical, 1 when they differ, 2 on error. Scripts that use `set -e` will abort on a normal "files differ" result — wrap with `|| true` or test the exit code explicitly.
  • `Compare-Object` is symmetric and emits objects, not text. The `<=` / `=>` SideIndicator shows which input the line belonged to; passing `-IncludeEqual` adds `==` lines.
  • Windows `fc` and Unix `diff` produce incompatible output formats — `fc` output cannot be fed to `patch` or `git apply`. For patch-compatible diffs on Windows, use Git's bundled `diff.exe` or WSL.
  • BSD `diff` (default on macOS pre-Sonoma) lacks `--color` and some GNU flags like `-y` (side-by-side) behave differently. Install GNU diffutils via Homebrew for parity.
  • Recursive `diff -r` follows symlinks by default — pass `--no-dereference` to compare the symlinks themselves.

WSL & PowerShell Core notes

pwshPowerShell Core has no `diff` cmdlet — `Compare-Object` is the structural equivalent and works the same on every platform. The `diff` alias for `Compare-Object` exists on Windows pwsh but is removed on Linux/macOS pwsh, so the system `/usr/bin/diff` resolves first there.
WSLWSL ships GNU diffutils. To diff a Windows-side file against a Linux-side file, use `diff /mnt/c/path/to/win.txt ~/linux.txt`; line-ending differences (CRLF vs LF) will show as full-file changes — pass `--strip-trailing-cr` to ignore them.

Common tasks using diff

Related commands