Linux `dos2unix` Command: Remove Hidden Windows Characters from Files
When you transfer text files between Windows and Linux systems, invisible formatting characters can silently corrupt your scripts, configuration files, and data pipelines. The root cause is a fundamental difference in how each operating system marks the end of a line: Windows uses a carriage return + line feed sequence (rn, also known as CRLF), while Linux expects only a line feed (n, or LF). That extra r character — invisible in most editors — can cause shell scripts to fail with cryptic errors, break configuration parsers, and produce unexpected output in text-processing tools like awk, sed, and grep.
The dos2unix utility was built specifically to solve this problem. It strips Windows-style CRLF line endings from text files and replaces them with Unix-style LF endings, making your files fully compatible with Linux toolchains. This guide covers everything you need to know: what dos2unix does under the hood, how to install it on major Linux distributions, its full syntax and options, and practical real-world examples.
—
What Is dos2unix and Why Does It Matter?
dos2unix is a lightweight, open-source command-line utility that converts text files from DOS/Windows format (CRLF line endings) to Unix/Linux format (LF line endings). It can also perform the reverse conversion (unix2dos), handle classic Mac line endings (CR only), and process binary-safe file operations.
Why Hidden r Characters Cause Real Problems
Consider a Bash script created or edited on a Windows machine. When you run it on Linux, you might see errors like:
bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directoryThat ^M is the visual representation of r. The shell cannot find the interpreter because the shebang line contains a hidden carriage return. Similarly, Python scripts, cron jobs, .env files, and Nginx or Apache configuration files can all behave unpredictably when they contain Windows line endings.
This is especially critical in server environments. If you manage a VPS Hosting environment or a Dedicated Server, deploying misconfigured scripts or corrupted config files can take down services. Knowing how to detect and fix line-ending issues is a fundamental sysadmin skill.
—
How to Install dos2unix on Linux
Most major Linux distributions include dos2unix in their default package repositories. Use the appropriate command for your distribution:
Debian / Ubuntu
sudo apt-get update && sudo apt-get install dos2unixCentOS / RHEL / AlmaLinux / Rocky Linux
sudo yum install dos2unixFedora
sudo dnf install dos2unixArch Linux
sudo pacman -S dos2unixopenSUSE
sudo zypper install dos2unixAfter installation, verify it is available:
dos2unix --version—
Detecting Windows Line Endings Before Converting
Before running dos2unix, it is good practice to confirm that a file actually contains CRLF line endings. Several methods work well:
Using file:
file filename.txtOutput for a Windows-format file will include CRLF line terminators.
Using cat -A:
cat -A filename.txtWindows line endings appear as ^M$ at the end of each line. Unix line endings show only $.
Using hexdump:
hexdump -C filename.txt | grep -i "0d 0a"The byte sequence 0d 0a is the hex representation of rn.
—
Full dos2unix Command Syntax
dos2unix [options] [input_file] [output_file]When called with only an input file, dos2unix converts the file in place, overwriting the original. When both an input and output file are specified, the original is preserved and the converted content is written to the new file.
—
dos2unix Options Reference
| Option | Long Form | Description |
|---|---|---|
-o | --oldfile | Convert files in old (in-place) mode — the default behavior |
-n | --newfile | Convert to a new file, preserving the original |
-c | --convmode | Set conversion mode: unix, dos, or mac |
-k | --keep-timestamp | Preserve the original file's modification timestamp |
-q | --quiet | Suppress all output messages and warnings |
-v | --verbose | Print verbose conversion information |
-l | --newline | Add an additional newline character |
-s | --safe | Skip binary files automatically |
-f | --force | Force conversion of binary files |
-b | --keep-bom | Keep the Byte Order Mark (BOM) if present |
-r | --remove-bom | Remove the Byte Order Mark (BOM) |
-V | --version | Display the version number and exit |
-h | --help | Display help information |
—
Practical Examples of Using dos2unix
1. Convert a Single File In Place
The most common use case — convert a file and overwrite it with the Unix-formatted version:
dos2unix filename.txtThe file is modified directly. No backup is created by default, so ensure you have a copy if needed.
—
2. Convert a File and Save to a New File
To preserve the original file and write the converted output to a separate file, use the -n flag (new file mode):
dos2unix -n filename.txt converted_filename.txtThis reads filename.txt, converts it, and writes the result to converted_filename.txt. The original remains untouched.
—
3. Convert Multiple Files at Once
You can pass multiple filenames in a single command:
dos2unix file1.txt file2.txt file3.txtAll listed files are converted in place. This is efficient for batch operations on a small set of known files.
—
4. Convert All .txt Files in a Directory
Use shell globbing or find to process entire directories:
dos2unix *.txtOr recursively across subdirectories:
find /path/to/directory -type f -name "*.txt" -exec dos2unix {} ;This is particularly useful when deploying web application files or configuration sets that were packaged on a Windows machine.
—
5. Convert All Shell Scripts Recursively
A common sysadmin task — fix all Bash scripts in a project directory:
find /var/www/myapp -type f -name "*.sh" -exec dos2unix {} ;—
6. Preserve the Original File Timestamp
By default, dos2unix updates the file's modification time. To keep the original timestamp (useful in deployment pipelines or when make relies on timestamps):
dos2unix -k filename.txt—
7. Quiet Mode — Suppress All Output
Ideal for use in shell scripts and automation where you do not want conversion messages cluttering logs:
dos2unix -q filename.txt—
8. Convert to Mac Line Endings (CR Only)
While rarely needed today, you can convert a file to classic Mac OS 9 format (carriage return only, r) using the -c conversion mode flag:
dos2unix -c mac filename.txt—
9. Convert from Unix Back to DOS Format
dos2unix ships alongside unix2dos, which performs the reverse operation — adding CRLF endings for Windows compatibility:
unix2dos filename.txt—
10. Remove the Byte Order Mark (BOM)
Files saved by Windows applications sometimes include a UTF-8 BOM at the beginning, which can break scripts and parsers on Linux. Remove it with:
dos2unix -r filename.txt—
Using dos2unix in Shell Scripts and Automation
dos2unix integrates cleanly into deployment scripts and CI/CD pipelines. Here is an example of a pre-deployment script that sanitizes all configuration and script files before they go live:
#!/bin/bash
# pre-deploy-sanitize.sh
# Converts all text files to Unix format before deployment
TARGET_DIR="/var/www/myapp"
echo "Sanitizing line endings in $TARGET_DIR..."
find "$TARGET_DIR" -type f ( -name "*.sh" -o -name "*.conf" -o -name "*.php" -o -name "*.py" ) | while read -r file; do
dos2unix -q -k "$file"
echo "Converted: $file"
done
echo "Done. All files converted to Unix format."Make the script executable and run it as part of your deployment workflow:
chmod +x pre-deploy-sanitize.sh
./pre-deploy-sanitize.sh—
Common Errors and Troubleshooting
dos2unix: command not found
The utility is not installed. Run the appropriate installation command for your distribution (see the installation section above).
dos2unix: Binary file ... is skipped
dos2unix detected what it believes is a binary file and skipped it. If you are certain the file is text, force conversion with:
dos2unix -f filenameScript Still Fails After Conversion
Verify the conversion worked:
file filename.shIt should now report ASCII text or UTF-8 Unicode text without mentioning CRLF. If issues persist, check for other encoding problems using hexdump.
Permission Denied
You may need elevated privileges to modify certain system files:
sudo dos2unix /etc/nginx/nginx.conf—
dos2unix vs. Alternative Methods
While dos2unix is the cleanest solution, experienced Linux administrators sometimes use other tools for quick one-off conversions:
Using sed:
sed -i 's/r//' filename.txtUsing tr:
tr -d 'r' < input.txt > output.txtUsing awk:
awk '{ sub("r$", ""); print }' filename.txt > output.txtUsing vim:
:set ff=unix
:wqThese alternatives work, but dos2unix is purpose-built for this task, handles edge cases (like BOM removal and binary file detection) more gracefully, and is the recommended tool for production use.
—
Relevance for Web Hosting and Server Management
Line-ending issues are not just a developer inconvenience — they are a genuine operational concern in hosted environments. PHP scripts with CRLF endings can produce unexpected whitespace in HTTP headers, causing session and cookie failures. Python WSGI applications may throw syntax errors. Nginx and Apache configuration files with hidden r characters can prevent services from starting entirely.
If you host websites or applications on a Shared Web Hosting plan or manage your own VPS with cPanel, incorporating dos2unix into your file upload and deployment workflow is a simple, high-value practice. For teams running automated deployments on Dedicated Servers, adding dos2unix to your CI/CD pipeline eliminates an entire class of environment-specific bugs before they reach production.
Additionally, if your infrastructure includes Email Hosting with custom scripts for mail processing or filtering, ensuring those scripts use correct Unix line endings is essential for reliable operation.
—
Quick Reference Summary
| Task | Command |
|---|---|
| Convert file in place | dos2unix filename.txt |
| Convert and save to new file | dos2unix -n input.txt output.txt |
| Convert multiple files | dos2unix file1.txt file2.txt file3.txt |
Convert all .sh files recursively | find . -name "*.sh" -exec dos2unix {} ; |
| Preserve original timestamp | dos2unix -k filename.txt |
| Quiet mode (no output) | dos2unix -q filename.txt |
| Remove BOM | dos2unix -r filename.txt |
| Convert to Mac format | dos2unix -c mac filename.txt |
| Reverse: Unix to DOS | unix2dos filename.txt |
| Check line endings | file filename.txt or cat -A filename.txt |
—
Conclusion
The dos2unix command is a small utility with an outsized impact in cross-platform development and server administration. Hidden r characters are one of the most common causes of "works on my machine" bugs when files move between Windows and Linux environments — and dos2unix eliminates them cleanly, safely, and efficiently.
By mastering its syntax and options, you can prevent deployment failures, ensure script compatibility, and maintain the integrity of configuration files across your entire infrastructure. Whether you are a developer pushing code to a Linux server, a sysadmin managing a fleet of machines, or a site owner uploading files to a hosting environment, making dos2unix part of your standard toolkit is a straightforward best practice that pays dividends every time files cross operating system boundaries.
