Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux dos2unix command

Linux dos2unix command

Linux Command Reference Linux Command Reference


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:


Why dos2unix Is Needed

Line Ending Differences

Different operating systems use different line endings:

Symptoms

When a text file created on Windows is opened on a Linux system:


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

  1. Binary Files: do not use dos2unix on binary files (such as images or PDFs), as this will corrupt the files
  2. Versioning: in version control systems such as Git, it is best to standardize line-ending handling
  3. Encoding issues: if a file contains non-ASCII characters, you may need to use the -c and -e option to handle encoding
  4. Backups: for important files, it is recommended to use the -b option 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:

Mastering the dos2unix command can effectively avoid various problems caused by line-ending differences and improve work efficiency.


Linux Command Reference Linux Command Reference