dos2unix 文件格式转换命令介绍

发布时间: 更新时间: 总字数:335 阅读时间:1m 作者: IP属地: 分享 复制网址

在windows平台上使用开发时,常常会有文件格式的问题,我们知道dos2unix可以完成从dos格式到unix格式的转换,本文提供一个批量转换的脚本。

安装rpm

yum install dos2unix -y

针对于单个文件或少量文件

使用vim 编辑sh脚本或者其他的脚本文件,然后按ESC进入vi的命令行模式,输入下面的命令:

:set fileformat=unix

当然如果要将文件格式设定为dos文件格式,也就是windows的格式,那么输入下面的命令就行了

:set fileformat=dos

修改完成后,使用下面的命令查看文件的格式类型:

:set ff

缺点:只适合少量文件,当文件数较多时,工作量巨大

批量文件

find -type f | xargs dos2unix -o

find -type f | xargs dos2unix --dos2unix --safe

脚本

#!/bin/sh
foreachd () {
  echo $1
  for file in $1/*
  do
    if [ -d $file ]
    then
      echo "directory $file"
      foreachd $file
    fi

    if [ -f $file ]
    then
      echo "file $file"
      dos2unix $file
      chmod -x $file
    fi

  done
}

echo $0
a=`echo $1`
if [ $# -gt 0 ]
then
  foreachd $a
else
  foreachd "."
fi

使用

dos2unix.sh [path]
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数