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

Linux unix2dos Command

Linux unix2dos Command

Linux Command Reference Linux Command Reference


unix2dos is a utility for converting text files from Unix/Linux format to DOS/Windows format. It mainly deals with line endings in text files.

On Unix/Linux systems, each line of a text file ends with a line feed (LF,n) ; on Windows systems, each line ends with a carriage return + line feed (CRLF,rn) . This difference can cause formatting problems when sharing text files between different systems.


Why do you need unix2dos

You may need to use unix2dos when working in the following scenarios:

  1. Cross-platform collaboration: when scripts or configuration files written on Linux need to be used on Windows
  2. File transfer: when transferring files from a Unix system to a Windows system via FTP or other means
  3. Tool compatibility: some Windows tools (such as Notepad) cannot correctly display line endings with only LF

Installing unix2dos

Most Linux distributions do not install unix2dos by default, but it can be easily installed via a package manager:

Debian/Ubuntu systems

Example

sudo

apt-get update

sudo

apt-get install

dos2unix

CentOS/RHEL systems

sudo yum install dos2unix

Note

unix2dos and dos2unix is usually included in the same software package; installing dos2unix the package installs both tools.


Basic Syntax

unix2dos [选项] 文件...

Common options

Option Description
-b or --keep-bom Preserve the UTF-8 byte order mark (BOM)
-c or --convmode Conversion mode (ascii, 7bit, iso, mac)
-f or --force Force conversion of binary files
-h or --help Display help information
-k or --keepdate Keep file timestamps unchanged
-L or --license Display software license
-l or --newline Only add line feeds
-m or --add-bom Add a UTF-8 byte order mark
-n or --newfile Write to a new file (do not modify the original)
-o or --oldfile Write to the original file (default behavior)
-q or --quiet Silent mode, do not show warnings
-V or --version Display version information

Usage examples

Example 1: Basic conversion

Convert a Unix-format file to DOS format:

unix2dos file.txt

This converts file.txt line endings from LF to CRLF, modifying the original file directly.

Example 2: Keep the original file and create a new one

If you want to keep the original file and create a new converted file:

unix2dos -n unixfile.txt dosfile.txt

Example 3: Convert multiple files in batch

Use wildcards to convert multiple files:

unix2dos *.txt

Example 4: Recursively convert all files in a directory

combined with find the command processes directories recursively:

find . -name "*.sh" -exec unix2dos {} ;

Example 5: Keep file timestamps unchanged

Convert a file without modifying its timestamp:

unix2dos -k file.txt


Practical Application Scenarios

Scenario 1: Running shell scripts on Windows

Suppose you have written a Linux shell script but need to run it in Git Bash or WSL on Windows:

unix2dos script.sh

Scenario 2: Using configuration files across platforms

When your application's configuration files need to be shared between Windows and Linux:

unix2dos config.properties

Scenario 3: Sharing documents with Windows users

Convert a Markdown file to Windows format so colleagues can edit it with Notepad:

unix2dos README.md


Notes

  1. Binary Files: avoid using unix2dos on binary files (such as images or archives) unless you know exactly what you are doing (you can use -f to force conversion)
  2. Back up files: when modifying original files directly, it is recommended to back up important files first
  3. UTF-8 BOM: Windows sometimes needs a BOM to correctly recognize UTF-8 encoding; you can use -m the option to add
  4. Reverse conversion: if you need to convert DOS format to Unix format, use dos2unix Command

Frequently Asked Questions

Q1: How do I check a file's line ending type?

Use file Command:

file filename.txt

Or use cat Show special characters:

cat -A filename.txt

Unix format shows $ at the end of lines, while DOS format shows ^M$.

Q2: Does the file size change after conversion?

Yes, because each line gains an extra carriage return (CR), the file usually becomes slightly larger.

Q3: Why does my script throw an error on Windows?

It may be because the line endings are incorrect; try converting with unix2dos and then running it.

Q4: How can I automatically detect and convert in a script?

You can combine with file commands and conditional checks:

Example

if

file

filename.txt

|

grep

-q

"CRLF"

;

then

   

echo

"File is already in DOS format"

else

    unix2dos filename.txt

fi


Summary

unix2dos is a simple yet powerful tool that solves the problem of cross-platform text file format compatibility. Through this article, you should be able to:

  1. understand the difference between Unix and DOS/Windows line endings
  2. correctly install and use the unix2dos command
  3. handle various file conversion scenarios
  4. avoid common conversion mistakes

Remember, in a cross-platform collaborative environment, keeping text file formats consistent can avoid many unnecessary problems.


Linux Command Reference Linux Command Reference