Linux(Centos 7.6)命令详解:dos2unix

王柳  论坛元老 | 2025-1-24 11:38:03 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1021|帖子 1021|积分 3063

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.命令安装

dos2unix 命令默认情况下是没有安装的,如设置yum源,可通过yum安装命令如下:
  1. yum install dos2unix
复制代码
dos2unix 有一个对立的命令unix2dos,也需要yum安装,一样平常使用不到这里不做过多解释,具体参数可以与以下参考对比理解。
2.命令作用

将Windows格式文件件转换为Unix、Linux格式的文件(也可以转换成其他格式的)
3.命令语法

Usage: dos2unix [options] [file ...] [-n infile outfile ...]
4.参数详解

options:


  • -c, --convmode,转换方式,支持ascii, 7bit, iso, mac,默认为ascii
  • -f, --force,欺压转换二进制文件
  • -k, --keepdate,保存输出文件日期
  • -l, --newline,添加额外的换行符
  • -n, --newfile,写入新文件
  • -o, --oldfile,写入旧文件(假如旧文件不存在会报错)
  • -q, --quiet,安静模式
  • -s, --safe,跳过二进制文件(默认)
  • -F, --follow-symlink,跟随符号链接文件转换链接实际文件(链接的实际文件会被转换)
  • -R, --replace-symlink,用转换后的文件替换符号链接(链接文件转换为实际文件且转换,原链接的实际文件不变)
  • -S, --skip-symlink,保持符号链接和目标不变(默认)
5.常用用例

注:以下测试转换文件是Windows上传至Centos的测试文件。
5.1.默认直接转换

  1. [root@node2 Desktop]# file test1.txt
  2. test1.txt: ASCII text, with CRLF line terminators
  3. [root@node2 Desktop]#
  4. [root@node2 Desktop]# dos2unix test1.txt
  5. dos2unix: converting file test1.txt to Unix format ...
  6. [root@node2 Desktop]#
  7. [root@node2 Desktop]# file test1.txt
  8. test1.txt: ASCII text
复制代码
5.2.静默直接转换

  1. [root@node2 Desktop]# file test2.txt
  2. test2.txt: ASCII text, with CRLF line terminators
  3. [root@node2 Desktop]#
  4. [root@node2 Desktop]# dos2unix -q test2.txt
  5. [root@node2 Desktop]#
  6. [root@node2 Desktop]# file test2.txt
  7. test2.txt: ASCII text
复制代码
5.3.保持文件日期转换

注:文件日期不会改变
  1. [root@node2 Desktop]# ll test3.txt
  2. -rwxr--r--. 1 root root 568 Jan 21 19:45 test3.txt
  3. [root@node2 Desktop]#
  4. [root@node2 Desktop]# file test3.txt
  5. test3.txt: ASCII text, with CRLF line terminators
  6. [root@node2 Desktop]#
  7. [root@node2 Desktop]# dos2unix -k test3.txt
  8. dos2unix: converting file test3.txt to Unix format ...
  9. [root@node2 Desktop]#
  10. [root@node2 Desktop]# file test3.txt
  11. test3.txt: ASCII text
  12. [root@node2 Desktop]#
  13. [root@node2 Desktop]# ll test3.txt
  14. -rwxr--r--. 1 root root 563 Jan 21 19:45 test3.txt
复制代码
5.4.转换到新文件

  1. [root@node2 Desktop]# file test4*
  2. test4.txt: ASCII text, with CRLF line terminators
  3. [root@node2 Desktop]#
  4. [root@node2 Desktop]# dos2unix -n test4.txt test4_1.txt
  5. dos2unix: converting file test4.txt to file test4_1.txt in Unix format ...
  6. [root@node2 Desktop]#
  7. [root@node2 Desktop]# file test4*
  8. test4_1.txt: ASCII text
  9. test4.txt:   ASCII text, with CRLF line terminators
复制代码
5.5.级联转换链接文件

  1. [root@node2 Desktop]# ll test5*
  2. lrwxrwxrwx. 1 root root   9 Jan 21 19:51 test5_ln1.txt -> test5.txt
  3. -rwxr--r--. 1 root root 568 Jan 21 19:45 test5.txt
  4. [root@node2 Desktop]# file test5*
  5. test5_ln1.txt: symbolic link to 'test5.txt'
  6. test5.txt:     ASCII text, with CRLF line terminators
  7. [root@node2 Desktop]#
  8. [root@node2 Desktop]# dos2unix -F test5_ln1.txt  ## 如不加-F参数,会跳过文件不转换
  9. dos2unix: converting file test5_ln1.txt to Unix format ...
  10. [root@node2 Desktop]#
  11. [root@node2 Desktop]# file test5*
  12. test5_ln1.txt: symbolic link to 'test5.txt'
  13. test5.txt:     ASCII text
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王柳

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表