Linux dos2unix Command

Linux 命令大全Linux Command Encyclopedia


dos2unix is a utility for converting text files from DOS/Windows format to Unix/Linux format. It mainly solves problems caused by differences in newline characters between different operating systems.

Core functionality:

  • Converts text files in DOS/Windows format (CRLF line endings) to Unix/Linux format (LF line endings)
  • Handles file encoding issues (optional)
  • Batch convert multiple files

Why do you need dos2unix

Newline character differences

Different operating systems use different newline characters:

  • Windows/DOS: uses carriage return + line feed (CRLF, i.e.rn)
  • Unix/Linux: only uses line feed (LF, i.e.n)
  • Mac OS (old versions): uses carriage return (CR, i.e.r)

Problem symptoms

When text files created on Windows are opened on a Linux system:

  • May appear^Mcharacters (visible in vi)
  • Script files may not execute properly
  • Some tools (such as grep, awk) may not process the text correctly

Installing 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 installation

dos2unix --version

Basic syntax

dos2unix [选项] 文件...

Common options

Option Description
-k Keep the file timestamp unchanged
-f Force conversion, ignore binary file checking
-q Quiet mode, do not display warning messages
-o Overwrite the original file (default behavior)
-n New file mode, keep the original file, output to a new file
-c Convert encoding (must be used with-eoption)
-e Specify the target encoding (such as-e utf8)
-b Create a backup file (add.baksuffix)

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; 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 but keep its modification time unchanged


Practical application scenarios

1. Fixing scripts transferred from Windows

Example

# Convert script file
dos2unix backup_script.sh

# Add execute permission
chmod +x backup_script.sh

# Execute the script
./backup_script.sh

2. Handling configuration files shared by the development team

Example

# Convert the entire configuration directory
find /etc/myapp/conf.d/ -type f -exec dos2unix {} ;

3. Preparing files to upload to a Linux server

dos2unix -b *.php  # 转换并创建备份

Precautions

  1. Binary files: Do not use dos2unix on binary files (such as images, PDFs); this will damage the file
  2. Version control: In version control systems such as Git, it is best to uniformly configure the handling of newline characters
  3. Encoding issues: If the file contains non-ASCII characters, you may need to use the-cand-eoption to handle the encoding
  4. Backup: It is recommended to use the-boption to create a backup before converting important files

Alternative solutions

If dos2unix is not installed on the system, you can use the following alternative methods:

1. Using the tr command

tr -d 'r'  unixfile.txt

2. Using the sed command

sed -i 's/r$//' winfile.txt

3. Using 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 newline character differences and improve work efficiency.


Linux 命令大全Linux Command Encyclopedia

Other extensions