Convert Windows CRLF Line Endings to Unix LF
Strip carriage returns from Windows-origin text, CSV, and script files.
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' 数据.txt02What 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
!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' 数据.txt05Sources
- perlrun – how to execute the Perl interpreterEvidence: perl -pi in-place edit is the standard line-ending conversion method on macOS
Last verified: 2026-09-18 · Category: Files & Folders
06Related solutions
Find files larger than 500 MB on Mac
List the biggest files in your home folder that are eating disk space.
Show file extensions for all files on Mac
Make Finder display .pdf, .docx, .png and every other extension at all times.
Find Which Process Has a File or Volume Open with lsof
List every open file descriptor — files, directories, and network sockets — per process.