如何将文件夹中的所有文件转换为不同的行尾?(在Windows上)


15

如何将文件夹中的所有文件转换为不同的行尾(Linux或Mac)?我正在使用Windows。

现在,我的方法是使用Winscp复制到linux框中,然后在该文件夹上递归运行dos2unix。


听起来您当前的方法还不错
Ramhound 2014年

Answers:


17

在Cygwin中安装了Cygwin和Dos2unix

find . -type f -exec dos2unix {} \;

1
您可能想要-maxdepth 1使其不会通过子目录
barlop 2014年

1
谢谢你的提示。但我的用例需要的所有文件,包括那些在子文件夹..
萨蒂什

对我来说,安装Cygwin只是为了运行一个程序似乎太过分了。我建议只使用同一程序的DOS版本,然后在外壳程序中使用一行命令对所有文件运行它。
理查德

1
@Richard:是的,但是我们许多人已经安装了Cygwin及其工具。您也可以尝试。它将使您的Windows更具Linux风格,因此更具容忍性。另外,您不必寻找针对Windows命令行问题的解决方案。
约翰·雷德

5

您可以在Windows上安装git bash而不是cygwin32(或类似文件)。它配备了几个类unix命令(例如,finddos2unix等)在bash仿真模式。安装后,将文件从Windows转换为Unix文件结尾应该很容易。

假设您将文件(后缀.java结束)放在名为src的文件夹树中,并且想要将其结尾从Windows转换为Unix。

  1. 在Windows资源管理器中找到src
  2. 右键单击并从Windows上下文菜单中打开该文件夹上的Git Bash。
  3. 运行:find . -name "*.java" -exec dos2unix {} \;

就是这样!


0

使用dos2unix和unix2dos的答案非常好。

这是根据您的答案的另一种方法。

尽管可以很有趣地使用Gnuwin32,但是具有命令的Gnuwin32软件包是Cygutils http://gnuwin32.sourceforge.net/packages/cygutils.htm, 因此您将在C:\ Program Files文件中获得unix2dos.exe和dos2unix.exe。 .... \ GnuWin32 \ bin并包含在您的PATH中。

C:\somedir>dir<ENTER>
 Volume in drive C has no label.
 Volume Serial Number is DC46-3C68

 Directory of C:\somedir

05/23/2014  01:10 AM    <DIR>          .
05/23/2014  01:10 AM    <DIR>          ..
05/23/2014  01:10 AM                 4 file1
               1 File(s)              4 bytes
               3 Dir(s)  196,129,951,744 bytes free

Do this command to go from dos2unix
C:\somedir>for %f in (*) do dos2unix %f <ENTER>

C:\somedir>dos2unix file1 <-- This runs automatically from you doing the above command
file1: done.

And do this command to go from unix2dos
C:\somedir>for %f in (*) do unix2dos %f <ENTER>

C:\somedir>unix2dos file1 <-- This runs automatically from you doing the above command
file1: done.

C:\somedir>

并测试您的%f是否会执行您想要的操作,然后使用echo或@ECHO,例如
在(*)中使用%f做@ECHO unix2dos%f

您可以使用xxd创建文件并测试它们是否已转换。Windows的xxd随附VIM C:\ Program“” Files \ vim \ vim74 \ xxd.exe

so, i'll create a file, I like this style of command is it allows me to 
create whateer file I want, a dos one or a unix one or anything.  
61 is hex for 'a'       
C:\somedir>echo 610d0a| xxd -r -p >testfile <ENTER>

check the file raw, in its hex
C:\somedir>cat testfile | xxd -p <ENTER>
610d0a

check the file in ascii or unicode
C:\somedir>cat testfile <ENTER>
a

and the following commands just prove that dos2unix and unix2dos work/are working fine.  
C:\somedir>dos2unix testfile <ENTER>
testfile: done.

C:\somedir>cat testfile | xxd -p <ENTER>
610a

C:\somedir>unix2dos testfile <ENTER>
testfile: done.

C:\somedir>cat testfile | xxd -p <ENTER>
610d0a

C:\somedir>

注意-同样在使用* nix发行版的情况下,查看apt-get(apt-cache搜索dos2unix),该软件包确实是dos2unix(也许与apt-get install dos2unix一起安装),并且包括dos2unix可执行文件,unix2dos可执行文件。如果执行apt-cache搜索unix2dos,则会显示dos2unix软件包。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.