Line endings

Updated: 08 February 2024

Write lines of text to file, with alternating line-ending type: CRLF, LF then finally LF. Use hexdump to verify the result

echo -n -e \\x61\\x0d\\x0a\\x62\\x0a\\x63\\x0a > file
cat file
hexdump --canonical file

Output

a                                                                    
b                                                                    
c                                                                    
00000000  61 0d 0a 62 0a 63 0a                              |a..b.c.|
00000007                                                             

Vim displays CR as ^M


\r\n DOS / Windows – carriage return + line feed.
CRLF DOS / Windows – carriage return + line feed.
0d 0a output of hexdump -C
0xD 0xA alternatively

\n Unix – line feed.
LF Unix – line feed.
0a output of hexdump -C
0xA alternatively


dos2unix

A program that converts plain text files in DOS/MAC format to UNIX format.

Recursively find all files in current directory and run dos2unix on each

find . -type f -exec dos2unix {} \;

unix2dos

A program that converts text files in UNIX format to DOS format.

Convert and replace a.txt. Convert and replace b.txt.

unix2dos a.txt b.txt

Leave a comment