Collaborating on software repositories, running shell scripts, or parsing CSV data files across heterogeneous environments containing both Windows and Linux systems often produces mysterious errors. Bash scripts fail to execute with
: command not found, configuration files break parsing daemons, and Git diffs flag every single line of code as modified even when no textual changes occurred.
These issues stem from fundamentally different line-break conventions. Windows operating systems terminate lines with a carriage return followed by a line feed (CRLF, represented as
or hex 0D 0A), whereas Linux and Unix-like systems terminate lines solely with a line feed (LF, represented as
or hex 0A). The dos2unix and unix2dos utilities provide a fast, dependable method for converting line endings in the Linux terminal.
Identifying Windows Line Endings in Linux
Because standard text editors often render CRLF and LF files identically on screen, you must inspect the raw control characters to verify line terminators:
# Inspect using the file utility
file deploy_script.sh
If the file contains Windows line breaks, the output explicitly states: ASCII text executable, with CRLF line terminators.
You can also use cat -v or head to reveal trailing carriage return characters represented as ^M:
cat -v deploy_script.sh
#!/bin/bash^M
echo "Deploying application..."^M
Installing dos2unix and unix2dos on Linux
The dos2unix package includes both dos2unix and unix2dos binaries across all major Linux distributions:
# Ubuntu, Debian, and Linux Mint
sudo apt update
sudo apt install dos2unix
# Fedora, RHEL, and CentOS
sudo dnf install dos2unix
# Arch Linux and Manjaro
sudo pacman -S dos2unix
Converting Windows CRLF to Unix LF Using dos2unix
To convert a script or configuration file to standard Linux line endings in place:
dos2unix deploy_script.sh
The utility strips the carriage returns, leaving clean LF terminators:
dos2unix: converting file deploy_script.sh to Unix format...
To preserve the original file as a backup before converting, use the -n (new file) flag:
dos2unix -n original_script.sh cleaned_script.sh
Converting Unix LF to Windows CRLF Using unix2dos
If you are preparing plain text log exports, documentation, or scripts intended for legacy Windows tools or Notepad applications that require CRLF endings:
unix2dos export_data.csv
The command converts all single LF characters into two-byte CRLF terminators.
Batch Converting Multiple Files and Directories
When dealing with an entire project directory containing dozens of converted files, you can process them in batch using shell globs or the find command:
# Convert all shell scripts in the current directory
dos2unix *.sh
# Recursively find and convert all Python and Shell files in a project tree
find . -type f ( -name "*.sh" -o -name "*.py" ) -exec dos2unix {} +
Handling File Encodings and BOM (Byte Order Marks)
Windows tools frequently prepend a UTF-8 Byte Order Mark (BOM) to files, which can cause shell interpreters to fail on the shebang line (#!/bin/bash). dos2unix automatically handles BOMs:
# Strip UTF-8 Byte Order Mark during conversion
dos2unix -r deploy_script.sh
dos2unix and unix2dos Command Options Reference
| Command / Flag | Purpose | Example Syntax |
|---|---|---|
dos2unix <file> |
Converts CRLF to LF in place. | dos2unix config.env |
unix2dos <file> |
Converts LF to CRLF in place. | unix2dos export.txt |
-n |
Writes output to a new file, keeping source intact. | dos2unix -n in.txt out.txt |
-k, --keepdate |
Preserves the original file modification timestamp. | dos2unix -k build.sh |
-q, --quiet |
Suppresses informational terminal output. | dos2unix -q *.sh |
Mastering dos2unix and unix2dos ensures smooth cross-platform workflows, preventing syntax errors and line-break inconsistencies across heterogeneous environments.