What it does
unix2dos converts text files from Unix format (LF, \n) to DOS/Windows format (CRLF, \r\n). This is often required when transferring scripts or text files to Windows environments.
How it works (mechanical)
- Scans file for linefeed characters (LF).
- Inserts carriage return (CR) before each LF.
- Modifies file in place unless redirected.
10 Practical Examples
# 1) Convert file in place unix2dos file.txt
# 2) Convert multiple files unix2dos *.txt
# 3) Convert and keep original unix2dos -n file.txt file_dos.txt
# 4) Quiet mode unix2dos -q file.txt
# 5) Batch convert directory
find . -name "*.sh" -exec unix2dos {} \;# 6) Combine with iconv for encoding fixes iconv -f ISO-8859-1 -t UTF-8 file.txt | unix2dos > newfile.txt
# 7) Verify line endings file file.txt
# 8) Convert script before emailing to Windows user unix2dos deploy.sh
# 9) Reverse operation (see dos2unix) dos2unix file.txt
# 10) Check visually cat -v file.txt
Notes & Gotchas
- Modifies files in place by default.
- May affect binary files if used incorrectly.
- Git can manage line endings automatically (core.autocrlf).
- Use dos2unix for reverse conversion.
Historical Context
Unix uses LF (\n) for line endings, while DOS/Windows historically used CRLF (\r\n). Tools like unix2dos were created to bridge that compatibility gap.
Related Commands
- dos2unix — reverse conversion
- iconv — encoding conversion
- file — detect file format
- sed — manual line ending adjustments