ASCII New Line: How to Identify LF, CR and CRLF in Your Text
Learn which ASCII characters make a new line (LF, CR, CRLF), how to reveal them with od, xxd, cat, file, grep or Python, and how to verify or convert line endings safely.
ASCII has no single "new line" character. A line break is stored as one of two invisible control characters, or as a pair of them: Line Feed (LF), Carriage Return (CR), or CR followed by LF (CRLF). Which one a file uses depends on where and how it was created. This guide shows how to identify the character behind each line break, how to verify what you find, and how to change it without altering the rest of your text.
The ASCII characters behind a line break
| Name | Abbreviation | Decimal | Hex | Escape sequence | Caret notation | Typical use |
| Line Feed | LF | 10 | 0x0A | \n | ^J | Unix, Linux and macOS text files |
| Carriage Return | CR | 13 | 0x0D | \r | ^M | Classic Mac OS (before OS X) used CR alone |
| Carriage Return + Line Feed | CRLF | 13 10 | 0x0D 0x0A | \r\n | ^M^J | Windows text files and many internet protocols, such as HTTP and SMTP headers |
Control characters have no visible glyph, so tools represent them in other ways. An escape sequence such as \n is how many programming languages and utilities write the character in source code or output. Caret notation such as ^M is an older terminal convention: the caret stands for the Control key, so ^M means Ctrl+M, which corresponds to code 13 (CR).
What inspecting line endings does for your text
A line-ending inspection takes any text file or string as input and produces a visible representation of the bytes at the end of each line: escape sequences, hex values or end-of-line markers. It answers one question: does this text use LF, CR, CRLF, or a mix? The inspection itself is read-only. Your content changes only if you run a separate conversion step.
This matters because mismatched line endings cause real problems. A shell script saved with CRLF can fail with an error such as "/bin/bash^M: bad interpreter". Stray ^M symbols can appear in terminal output, and diffs can flag every line as changed when only the line endings differ.
Step by step: identify the newline character
Step 1: Show each character with od
The od utility (octal dump) with the -c option prints every byte as a character or an escape. The example below pipes a short sample containing one LF line and one CRLF line into od.
printf 'hi\nthere\r\n' | od -c
Illustrative output (this is not captured output, and spacing may differ by system):
0000000 h i \n t h e r e \r \n
0000012
In this output, \n marks an LF and \r marks a CR. A \r directly before \n is a CRLF line ending. To inspect a file instead of a sample, run: od -c file.txt
Step 2: Read the hex values with xxd or hexdump
A hex dump shows the raw byte values. Look for 0a (LF) and for the pair 0d 0a (CRLF). A 0d that is not followed by 0a is a lone CR.
xxd file.txt
hexdump -C file.txt
Step 3: Mark line ends with cat
On systems with GNU coreutils, cat -A prints a $ at the end of each line and shows a CR as ^M. On BSD and macOS, cat -e gives a similar display.
cat -A file.txt
Illustrative output for a file whose first line ends in LF and whose second line ends in CRLF:
hi$
there^M$
Step 4: Get a quick summary with file
The file command often reports the line terminators, for example "with CRLF line terminators" or "with CR line terminators". A file that uses plain LF usually gets no such note. The exact wording varies by version.
file file.txt
Step 5: Count lines that contain a CR
In bash and similar shells, $'\r' expands to a literal CR character. grep -c counts the lines that contain it. Any result above zero means CR characters are present.
grep -c $'\r' file.txt
Optional: count each ending type in Python
Open the file in binary mode ("rb"). In text mode, Python translates line endings by default, which would hide the information you are looking for.
with open("file.txt", "rb") as f:
data = f.read()
crlf = data.count(b"\r\n")
print(crlf, "CRLF")
print(data.count(b"\n") - crlf, "LF only")
print(data.count(b"\r") - crlf, "CR only")
Verify the result
Run one of the checks above and read the result:
- Only LF counts above zero: the file uses LF.
- Only CRLF counts above zero: the file uses CRLF.
- Only CR-only counts above zero: the file uses lone CR.
- More than one count above zero: the file has mixed line endings.
After any conversion, run the same check again. Comparing before and after with the same tool is the simplest way to confirm that only the line endings changed. Converting CRLF to LF removes exactly one byte per converted line, so the file size should shrink by that number of bytes.
Changing line endings without damaging content
Make a backup before converting a file. The dos2unix and unix2dos utilities, where installed, convert between CRLF and LF. GNU sed can strip a CR only when it appears at the end of a line. BSD and macOS sed need -i '' instead of -i. The tr command removes every CR in the file, including any that are not part of a line ending, so use it only when you are sure no other CRs carry meaning.
cp file.txt file.txt.bak
dos2unix file.txt
unix2dos file.txt
sed -i 's/\r$//' file.txt
tr -d '\r' < file.txt > file-lf.txt
Common sources of confusion
- Escape sequence versus stored bytes: writing \n in code produces an LF, but some environments translate it on output. In C and Python, text-mode file writes on Windows convert \n to CRLF.
- Version control: Git settings such as core.autocrlf can change line endings when files are checked out or committed. Always inspect the actual file in your working directory, not your assumption about it.
- Editors: many code editors show the current line-ending mode, such as LF or CRLF, in the status bar and let you switch it. That display reflects the editor's detection, so confirm it with a byte-level check if the result matters.
- Line breaks beyond ASCII: Unicode defines further line-break characters, including NEL (U+0085, encoded in UTF-8 as C2 85), LINE SEPARATOR (U+2028, E2 80 A8) and PARAGRAPH SEPARATOR (U+2029, E2 80 A9). Searching only for 0x0A and 0x0D will miss them. In a hex dump, look for those byte sequences.
Gaps in this guide
No tool-specific test results or version documentation were supplied for this article. The commands describe the standard documented behavior of common Unix utilities and Python, and the sample outputs are illustrative rather than captured. Options and output wording can differ between GNU, BSD and macOS versions, so check your system's manual pages, for example by running man od or man file.