Recursively convert line endings dos2unix / mac2unix

To recursively convert line endings from DOS \r\n and old macOS \r to Unix \n format use dos2unix and mac2unix commands. Source code files shared among groups might end up with DOS line endings, which can cause issues across different operating systems. When viewed with “git diff” or similar, files might have ^M characters at the end of lines, indicating DOS or old macOS line endings. It’s important not to blindly use dos2unix or other mass conversion utilities on a recursive subtree, as the .git/ directory files could be corrupted.

Install dos2unix and mac2unix:

  • Windows: winget install waterlan.dos2unix Microsoft.Coreutils -e
  • macOS: brew install dos2unix
  • Linux: apt install dos2unix or similar package manager command

Git-tracked files

git ls-files -z | xargs -0 dos2unix -e

Set Git to use the correct line endings to avoid line ending conflicts in Git repositories.

Non Git-tracked files

For non Git-tracked files, use findutils “find” to recursively convert line endings:

find . -not \( -path "./.git" -type d -prune \) -type f -exec dos2unix -e {} + -exec mac2unix -e {} +