dos2unix

file managementLinux/Unix
The dos2unix command is one of the most frequently used commands in Linux/Unix-like operating systems. dos2unix Convert text file line endings from DOS/Windows format to Unix format

Quick Reference

Command Name:

dos2unix

Category:

file management

Platform:

Linux/Unix

Basic Usage:

dos2unix [options] [arguments]

Common Use Cases

    Syntax

    dos2unix [options] [file ...]

    Options

    Option Description
    -ascii Convert only line breaks (default)
    -iso Conversion between DOS and ISO-8859-1 character set
    -1252 Use Windows code page 1252 (Western European)
    -b, --keep-bom Keep Byte Order Mark
    -f, --force Force conversion of binary files
    -h, --help Display help information
    -k, --keepdate Keep output file date
    -L, --license Display program license
    -n, --newfile input output New file mode, convert input to output
    -o, --oldfile file Old file mode, convert file in place (default)
    -q, --quiet Quiet mode, suppress all warnings
    -v, --verbose Verbose operation

    Examples

    How to Use These Examples

    The examples below show common ways to use the dos2unix command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    dos2unix file.txt
    Convert a single file from DOS to Unix format.
    dos2unix -n input.txt output.txt
    Convert input.txt to Unix format and write to output.txt.
    dos2unix *.txt
    Convert multiple text files in the current directory. # Advanced Examples Advanced dos2unix -b file.txt Create a backup of original file before conversion. dos2unix -k file.txt Keep the original file date and time. dos2unix -f file.txt Force conversion even if file has Unix line endings. dos2unix -q file.txt Quiet mode, suppress all warnings and messages. find . -name "*.txt" -exec dos2unix {} \; Recursively convert all .txt files in current directory and subdirectories.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Dos2unix Command Overview: The dos2unix command converts text files from DOS/Windows format to Unix format by changing line endings. In DOS/Windows text files, lines end with both a carriage return (CR, \r) and a line feed (LF, \n) character, while in Unix/Linux systems, lines end with just a line feed (LF, \n). Line Ending Differences: - DOS/Windows: CR+LF (\r\n, 0x0D 0x0A) - Unix/Linux: LF (\n, 0x0A) - Mac (pre-OS X): CR (\r, 0x0D) Key Features: - Handles text files of various encodings - Can process multiple files at once - Provides options for backup and preservation of file attributes - Offers both in-place conversion and new file creation modes - Can handle UTF-8, UTF-16, and other encodings with BOM (Byte Order Mark) Common Use Cases: - Converting scripts or configuration files from Windows to work on Linux - Preparing text files for use in Linux applications - Fixing line ending issues in source code - Normalizing text files in cross-platform development environments - Preparing files for version control systems Related Commands: - unix2dos: Performs the opposite conversion (Unix to DOS format) - mac2unix: Converts from Mac format to Unix format - unix2mac: Converts from Unix format to Mac format Note: Modern text editors often have the ability to detect and work with different line ending formats, but command-line tools and scripts typically expect the line endings native to the platform they're running on.

    Related Commands

    These commands are frequently used alongside dos2unix or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the dos2unix command works in different scenarios.

    $ dos2unix
    View All Commands