Find and Convert Files Ending With CRLF on Linux

You can use the find command in Linux to search for files ending with CRLF, and the dos2unix command to convert those files to use LF line endings. CRLF (Carriage Return + Line Feed) line endings are commonly used in Windows systems, while LF (Line Feed) endings are standard on Linux systems.

Searching for Files With CRLF Endings

The most efficient way to find files with CRLF line endings is using grep with the -r (recursive) and -l (list filenames only) options

grep -rl $'\r' /path/to/search

This command searches recursively through the specified directory and prints only the names of files containing carriage return characters.

Alternative Method Using find and grep

For more control over the search, you can combine find with grep

find /path/to/search -type f -exec grep -Iq . {} \; -and -exec grep -Il $'\r' {} +

This command first checks if files are not empty, then searches for carriage return characters, making it more precise for text files.

Using cat Command for Detailed Analysis

To see exactly where CRLF line endings occur in files, use cat with line numbers

find /path/to/search -type f -exec sh -c 'cat -n "$1" | grep $'\r'' {} \;

This displays line numbers where CRLF endings are found, useful for debugging specific files.

Converting CRLF to LF

Using dos2unix Command

The dos2unix utility is the most straightforward tool for converting line endings. To convert a single file

dos2unix file.txt

To convert multiple files recursively

find /path/to/search -type f -name "*.*" -exec dos2unix {} +

Using sed Command

If dos2unix is not available, you can use sed to perform the conversion

sed -i 's/\r<br>/<br>/g' file.txt

The -i option edits files in-place. For safety, create backups by specifying a suffix

sed -i.bak 's/\r<br>/<br>/g' file.txt

To convert multiple files recursively using sed

find /path/to/search -type f -exec sed -i 's/\r<br>/<br>/g' {} +

Complete Workflow Example

Here's a practical example combining search and conversion

# First, find files with CRLF endings
grep -rl $'\r' /home/user/documents

# Convert them using dos2unix
grep -rl $'\r' /home/user/documents | xargs dos2unix

# Or use sed if dos2unix is unavailable
grep -rl $'\r' /home/user/documents | xargs sed -i 's/\r<br>/<br>/g'

Installation Notes

If dos2unix is not installed on your system, install it using your distribution's package manager

# Ubuntu/Debian
sudo apt-get install dos2unix

# CentOS/RHEL/Fedora
sudo yum install dos2unix
# or
sudo dnf install dos2unix

Conclusion

Finding and converting CRLF line endings on Linux can be accomplished using various methods. The grep command efficiently identifies files with CRLF endings, while dos2unix and sed provide reliable conversion options. Always create backups before converting files to ensure you can revert changes if needed.

Updated on: 2026-03-17T09:01:38+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements