Linux dos2unix command
dos2unix is a utility for converting text files from DOS/Windows format to Unix/Linux format. It mainly solves problems caused by differences in line endings between different operating systems.
Core Features:
- Convert DOS/Windows format text files (CRLF line endings) to Unix/Linux format (LF line endings)
- Handle file encoding issues (optional)
- Batch convert multiple files
Why dos2unix Is Needed
Line Ending Differences
Different operating systems use different line endings:
- Windows/DOS: uses carriage return + line feed (CRLF, i.e.,
rn) - Unix/Linux: uses only line feed (LF, i.e.,
n) - Mac OS (older versions): uses carriage return (CR, i.e.,
r)
Symptoms
When a text file created on Windows is opened on a Linux system:
- May appear as
^Ma character (visible in vi) - Script files may fail to execute properly
- Some tools (such as grep, awk) may not process the text correctly
Install dos2unix
Most Linux distributions do not install dos2unix by default; it needs to be installed manually:
Ubuntu/Debian
Example
sudo
apt-get update
sudo
apt-get install
dos2unix
CentOS/RHEL
sudo yum install dos2unix
Verify the installation
dos2unix --version
Basic Syntax
dos2unix [选项] 文件...
Common options
| Option | Description |
|---|---|
-k |
Preserve file timestamps |
-f |
Force conversion, ignoring binary file checks |
-q |
Silent mode, do not display warnings |
-o |
Overwrite the original file (default behavior) |
-n |
New file mode, keep the original file and output to a new file |
-c |
Convert encoding (requires the -e option) |
-e |
Specify the target encoding (e.g., -e utf8) |
-b |
Create a backup file (add .bak a suffix) |
Usage examples
1. Basic Conversion
dos2unix file.txt
This will directly modify the file.txt file
2. Keep the Original File
dos2unix -n file.txt newfile.txt
The original file remains unchanged, and the conversion result is saved to newfile.txt
3. Batch Conversion
dos2unix *.txt
Convert all .txt files in the current directory
4. Recursively Convert Directories
find . -type f -name "*.sh" -exec dos2unix {} ;
Convert all .sh files in the current directory and its subdirectories
5. Preserve Timestamps
dos2unix -k script.py
Convert script.py while keeping its modification time unchanged
Practical Application Scenarios
1. Fix Scripts Transferred from Windows
Example
Convert the script file
dos2unix backup_script.sh
Add execute permission
chmod
+x backup_script.sh
Executing scripts
.
/
backup_script.sh
2. Handle Configuration Files Shared by a Development Team
Example
Convert the entire configuration directory
find
/
etc
/
myapp
/
conf.d
/
-type
f
-exec
dos2unix
{
}
;
3. Prepare Files for Upload to a Linux Server
dos2unix -b *.php # 转换并创建备份
Notes
- Binary Files: do not use dos2unix on binary files (such as images or PDFs), as this will corrupt the files
- Versioning: in version control systems such as Git, it is best to standardize line-ending handling
- Encoding issues: if a file contains non-ASCII characters, you may need to use the
-cand-eoption to handle encoding - Backups: for important files, it is recommended to use the
-boption to create a backup before conversion
Alternatives
If dos2unix is not installed on your system, you can use the following alternative methods:
1. Use the tr Command
tr -d 'r' unixfile.txt
2. Use the sed Command
sed -i 's/r$//' winfile.txt
3. Use vim
Example
:
set
ff
=unix
:
wq
Summary
dos2unix is a practical tool for handling cross-platform text file format issues, especially suitable for:
- System administrators maintaining servers
- Developers collaborating across different platforms
- Handling scripts and configuration files migrated from Windows to Linux
Mastering the dos2unix command can effectively avoid various problems caused by line-ending differences and improve work efficiency.
Linux Command Reference