Line Endings

Managing line ending conventions across different operating systems including LF, CRLF, and CR formats

Line endings are special characters that mark the end of a line in text files. Different operating systems use different line ending conventions, which can cause compatibility issues when transferring files between Linux, Windows, and macOS systems.

Key Concepts

  • LF (Line Feed): Unix/Linux standard (\n, ASCII 10)
  • CRLF (Carriage Return + Line Feed): Windows standard (\r\n, ASCII 13+10)
  • CR (Carriage Return): Classic Mac standard (\r, ASCII 13)
  • Binary vs Text Mode: How systems interpret line endings

Detection Commands

file filename - Shows file type and line endings hexdump -C filename | head - View raw bytes od -c filename - Display characters including control chars

Common Tools

dos2unix - Convert Windows to Unix line endings unix2dos - Convert Unix to Windows line endings mac2unix - Convert Mac to Unix line endings sed - Stream editor for line ending conversion

Practical Examples

Example 1: Check line endings

1
2
file myfile.txt
# Output: myfile.txt: ASCII text, with CRLF line terminators

Identifies Windows-style line endings in file

Example 2: Convert Windows to Linux

1
2
dos2unix myfile.txt
# Converts CRLF to LF in-place

Fixes Windows files for Linux compatibility

Example 3: Convert with backup

1
2
dos2unix -b myfile.txt
# Creates myfile.txt.bak before conversion

Safely converts while preserving original

Example 4: Using sed for conversion

1
2
sed 's/\r$//' winfile.txt > unixfile.txt
# Removes carriage returns manually

Alternative method using sed command

Example 5: Batch conversion

1
2
find . -name "*.txt" -exec dos2unix {} \;
# Convert all .txt files recursively

Process multiple files at once

Visual Detection

1
2
3
cat -A filename.txt
# Shows ^M for carriage returns
# $ marks actual line endings

Use Cases

  • File Transfer: Between different OS platforms
  • Version Control: Git repositories with mixed contributors
  • Script Compatibility: Shell scripts from Windows
  • Data Processing: CSV files from different sources
  • Web Development: HTML/CSS files across platforms

tr - Character translation and deletion hexdump - Display file contents in hex format od - Octal dump of file contents cat -A - Show all characters including non-printing

Tips & Troubleshooting

Common Issues

  • Scripts failing with “command not found” errors
  • Extra characters appearing in processed text
  • Git showing entire files as changed

Quick Fixes

1
2
3
4
5
# Remove all carriage returns
tr -d '\r' < input.txt > output.txt

# Check for mixed line endings
grep -P '\r\n' filename || echo "Unix format"

Prevention

  • Configure editors to use Unix line endings
  • Set Git autocrlf settings appropriately
  • Use consistent development environments

Git Configuration

1
2
3
4
5
# For Windows users in mixed repos
git config --global core.autocrlf true

# For Linux/Mac users
git config --global core.autocrlf input