YouTip LogoYouTip

Linux Comm Dos2Unix

[![Image 1: Linux Command Encyclopaedia](#) Linux Command Complete Reference](#)\n\n* * *\n\ndos2unix is a utility tool used to convert text files from DOS/Windows format to Unix/Linux format. It mainly solves the problems caused by line ending differences between different operating systems.\n\n**Core Features**:\n\n* Convert DOS/Windows format text files (CRLF line endings) to Unix/Linux format (LF line endings)\n* Handle file encoding issues (optional)\n* Batch convert multiple files\n\n* * *\n\n## Why Need dos2unix\n\n### Line Ending Differences\n\nDifferent operating systems use different line endings:\n\n* **Windows/DOS**: Uses carriage return + line feed (CRLF, i.e., `rn`)\n* **Unix/Linux**: Uses only line feed (LF, i.e., `n`)\n* **Mac OS (older versions)**: Uses carriage return (CR, i.e., `r`)\n\n### Problem Symptoms\n\nWhen text files created on Windows are opened on Linux systems:\n\n* May display `^M` characters (visible in vi)\n* Script files may fail to execute properly\n* Some tools (like grep, awk) may not process text correctly\n\n* * *\n\n## Install dos2unix\n\nMost Linux distributions do not have dos2unix installed by default, manual installation is required:\n\n### Ubuntu/Debian\n\n## Example\n\nsudo apt-get update\n\nsudo apt-get install dos2unix\n\n### CentOS/RHEL\n\nsudo yum install dos2unix\n\n### Verify Installation\n\ndos2unix --version\n\n* * *\n\n## Basic Syntax\n\ndos2unix file...\n\n### Common Options\n\n| Option | Description |\n| --- | |\n| `-k` | Keep file timestamp unchanged |\n| `-f` | Force conversion, ignore binary file check |\n| `-q` | Quiet mode, suppress warning messages |\n| `-o` | Overwrite original file (default behavior) |\n| `-n` | New file mode, keep original file and output to new file |\n| `-c` | Convert encoding (must be used with `-e` option) |\n| `-e` | Specify target encoding (e.g., `-e utf8`) |\n| `-b` | Create backup file (adds `.bak` suffix) |\n\n* * *\n\n## Usage Examples\n\n### 1. Basic Conversion\n\ndos2unix file.txt\nThis will directly modify the file.txt file\n\n### 2. Keep Original File\n\ndos2unix -n file.txt newfile.txt\nOriginal file remains unchanged, conversion result saved to newfile.txt\n\n### 3. Batch Conversion\n\ndos2unix *.txt\nConvert all .txt files in current directory\n\n### 4. Recursive Directory Conversion\n\nfind . -type f -name "*.sh" -exec dos2unix {} ;\nConvert all .sh files in current directory and subdirectories\n\n### 5. Preserve Timestamp\n\ndos2unix -k script.py\nConvert script.py but keep its modification time unchanged\n\n* * *\n\n## Practical Application Scenarios\n\n### 1. Fix Scripts Transferred from Windows\n\n## Example\n\n# Convert script file\n\n dos2unix backup_script.sh\n\n# Add execute permission\n\nchmod +x backup_script.sh\n\n# Execute script\n\n ./backup_script.sh\n\n### 2. Handle Configuration Files Shared by Development Team\n\n## Example\n\n# Convert entire configuration directory\n\nfind/etc/myapp/conf.d/-type f -exec dos2unix {} ;\n\n### 3. Prepare Files for Upload to Linux Server\n\ndos2unix -b *.php # Convert and create backup\n\n* * *\n\n## Important Notes\n\n1. **Binary Files**: Do not use dos2unix on binary files (such as images, PDFs), this will corrupt the files\n2. **Version Control**: In version control systems like Git, it's best to uniformly configure line ending handling\n3. **Encoding Issues**: If files contain non-ASCII characters, you may need to use `-c` and `-e` options to handle encoding\n4. **Backup**: It is recommended to use `-b` option to create backups before converting important files\n\n* * *\n\n## Alternative Solutions\n\nIf dos2unix is not installed on the system, you can use the following alternative methods:\n\n### 1. Using tr Command\n\ntr -d 'r' unixfile.txt\n\n### 2. Using sed Command\n\nsed -i 's/r$//' winfile.txt\n\n### 3. Using vim\n\n## Example\n\n:set ff=unix\n\n:wq\n\n* * *\n\n## Summary\n\ndos2unix is a practical tool for handling cross-platform text file format issues, especially suitable for:\n\n* System administrators maintaining servers\n* Developers collaborating across different platforms\n* Handling scripts and configuration files migrated from Windows to Linux\n\nMastering the dos2unix command can effectively avoid various problems caused by line ending differences and improve work efficiency.\n\n* * Linux Command Complete Reference](#)
← Linux Comm IotopLinux Comm Jq β†’