" MicromOne: How to Convert CRLF Line Endings in Files Using the Terminal

Pagine

How to Convert CRLF Line Endings in Files Using the Terminal

 

When working with text files across different operating systems, one common issue is the difference in how line endings are represented. Windows uses CRLF (\r\n) while Unix/Linux systems use LF (\n). This can cause problems in scripts, code, or data files if not handled correctly.

What Are CRLF and LF?

  • CRLF (Carriage Return + Line Feed): Windows-style line ending. Two characters: \r\n.

  • LF (Line Feed): Unix/Linux-style line ending. One character: \n.

Why Convert CRLF?

  • Ensure compatibility of scripts and configuration files on Unix/Linux.

  • Avoid issues in version control systems like Git, where inconsistent line endings can cause unnecessary diffs.

  • Maintain proper formatting when sharing files across platforms.

How to Detect CRLF in Files

Use the file command:

file filename.txt

If the output mentions “CRLF,” the file uses Windows line endings.

Convert CRLF to LF Using Terminal Commands

1. Using dos2unix

The easiest way:

dos2unix filename.txt

This converts CRLF to LF in place.

If you don’t have dos2unix installed:

  • On Debian/Ubuntu:

sudo apt-get install dos2unix
  • On macOS (with Homebrew):

brew install dos2unix

2. Using sed

sed -i 's/\r$//' filename.txt

This command removes the \r (carriage return) at the end of each line.

3. Using tr

tr -d '\r' < inputfile > outputfile

Removes all carriage returns, writing the result to a new file.

Convert LF to CRLF (Unix to Windows)

Sometimes you need to add CRLF endings:

unix2dos filename.txt

Or with sed:

sed 's/$/\r/' filename.txt > outputfile.txt

Automate Conversion in Scripts

To batch convert all .txt files in a directory from CRLF to LF:

for file in *.txt; do dos2unix "$file"; done


Understanding and converting CRLF line endings is essential for cross-platform compatibility. Using simple terminal tools like dos2unix, sed, or tr, you can easily handle these conversions and avoid common pitfalls in file handling.