Convert Windows CRLF Line Endings to Unix LF

Strip carriage returns from Windows-origin text, CSV, and script files.

macOStextutilcrlfFilesencodingMedium risk
Run `perl -pi -e 's/\r\n/\n/g' file.txt` to rewrite the file in place with Unix endings.

01Command

Command
$ perl -pi -e 's/\r\n/\n/g' 数据.txt

02What it does

Windows ends lines with CRLF while macOS/Linux use LF, which breaks shell scripts, diff output, and parsers fed Windows files. In-place `perl -pi` edits every line without creating a temp file (dos2unix is not installed by default on macOS). To convert the other way, add `\r` before each `\n` instead.

03Risk

RiskMedium risk
!Warning: In-place editing has no undo — copy the file first for anything important. Never convert binary files (images, docx): rewriting them corrupts the format.

04Undo

Undo
$ cp 数据.txt 数据.txt.bak 2>/dev/null; perl -pi -e 's/(?<!\r)\n/\r\n/g' 数据.txt

05Sources

Last verified: 2026-09-18 · Category: Files & Folders

06Related solutions