dos2unix / unix2dos - Text file format converters

发布时间 2023-03-30 13:13:21作者: sinferwu

 

dos2unix / unix2dos - Text file format converters (sourceforge.io)

 

 dos2unix download | SourceForge.net

说明

Dos2unix软件包包括工具dos2unix 和 unix2dos,用于将纯文本文件在DOS或Mac格式与Unix格式之间相互转换。

DOS/Windows的文本文件中,断行符是由两个字符共同表示的:回车符(CR)和换行符(LF)。Unix的文本文件中,换行符则由单个换行符(LF)表示。而Mac的文本文件则由单个回车符(CR,用于Mac OS X之前的系统)或单个换行符(LF,用于当下的新Mac OS)表示。

除了断行符,Dos2unix还可以转换文件编码。一些DOS编码页可以被转换为Unix Latin-1,Windows Unicode(UTF-16)文件也可以被转换为Unix Unicode(UTF-8)文件。

二进制文件则会被自动跳过,除非指定了强制转换选项。

特殊文件,如目录和队列,会被自动跳过。

符号链接和其所指向的目标默认不会被转换。可以用选项来指定替换符号链接,或者将输出写入到链接目标。Windows下不支持写入到符号链接的目标。

Dos2unix由SunOS/Solaris下的版本改写而成。这两个版本间有一个重大差异:本版本默认进行原位转换(旧文件模式),而原来SunOS/Solaris下的版本只支持配对转换(新文件模式),参见选项 -o 和 -n。还有一个区别是SunOS/Solaris下的版本默认使用 iso 模式,而本版本默认使用 ascii 模式。

 

 

 

递归转换

结合 find(1) 和 xargs(1) 使用 dos2unix 可以递归地转换目录树中的文本文件。例如,转换当前目录的目录树中所有的 .txt 文件:

    dos2unix < a.txt
    cat a.txt | dos2unix

若文件名中有空格或引号,则需要使用 find(1) 选项 -print0 及相应的 xargs(1) 选项 -0;其他情况下则可以省略它们。也可以结合 -exec 选项来使用 find(1):

    find . -name '*.txt' -exec dos2unix {} \;

在Windows命令提示符中,可以使用下列命令:

    for /R %G in (*.txt) do dos2unix "%G"

PowerShell用户可以在Windows PowerShell中使用如下命令:

    get-childitem -path . -filter '*.txt' -recurse | foreach-object {dos2unix $_.Fullname}

RECURSIVE CONVERSION

In a Unix shell the find(1) and xargs(1) commands can be used to run dos2unix recursively over all text files in a directory tree. For instance to convert all .txt files in the directory tree under the current directory type:

    find . -name '*.txt' -print0 |xargs -0 dos2unix

The find(1) option -print0 and corresponding xargs(1) option -0 are needed when there are files with spaces or quotes in the name. Otherwise these options can be omitted. Another option is to use find(1) with the -exec option:

    find . -name '*.txt' -exec dos2unix {} \;

In a Windows Command Prompt the following command can be used:

    for /R %G in (*.txt) do dos2unix "%G"

PowerShell users can use the following command in Windows PowerShell:

    get-childitem -path . -filter '*.txt' -recurse | foreach-object {dos2unix $_.Fullname}