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
|
|
Identifies Windows-style line endings in file
Example 2: Convert Windows to Linux
|
|
Fixes Windows files for Linux compatibility
Example 3: Convert with backup
|
|
Safely converts while preserving original
Example 4: Using sed for conversion
|
|
Alternative method using sed command
Example 5: Batch conversion
|
|
Process multiple files at once
Visual Detection
|
|
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
Related Commands
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
|
|
Prevention
- Configure editors to use Unix line endings
- Set Git autocrlf settings appropriately
- Use consistent development environments
Git Configuration
|
|