Linux(Centos 7.6)命令详解:dos2unix

1.命令作用

将Windows格式文件件转换为Unix、Linux格式的文件(也可以转换成其他格式的)

2.命令语法

Usage: dos2unix [options] [file ...] [-n infile outfile ...]

3.参数详解

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,保持符号链接和目标不变(默认)

4.常用用例

**注:**以下测试转换文件是Windows上传至Centos的测试文件。

4.1.默认直接转换

bash 复制代码
[root@node2 Desktop]# file test1.txt 
test1.txt: ASCII text, with CRLF line terminators
[root@node2 Desktop]# 
[root@node2 Desktop]# dos2unix test1.txt 
dos2unix: converting file test1.txt to Unix format ...
[root@node2 Desktop]# 
[root@node2 Desktop]# file test1.txt 
test1.txt: ASCII text

4.2.静默直接转换

bash 复制代码
[root@node2 Desktop]# file test2.txt 
test2.txt: ASCII text, with CRLF line terminators
[root@node2 Desktop]# 
[root@node2 Desktop]# dos2unix -q test2.txt 
[root@node2 Desktop]# 
[root@node2 Desktop]# file test2.txt 
test2.txt: ASCII text

4.3.保持文件日期转换

**注:**文件日期不会改变

bash 复制代码
[root@node2 Desktop]# ll test3.txt 
-rwxr--r--. 1 root root 568 Jan 21 19:45 test3.txt
[root@node2 Desktop]# 
[root@node2 Desktop]# file test3.txt 
test3.txt: ASCII text, with CRLF line terminators
[root@node2 Desktop]# 
[root@node2 Desktop]# dos2unix -k test3.txt 
dos2unix: converting file test3.txt to Unix format ...
[root@node2 Desktop]# 
[root@node2 Desktop]# file test3.txt 
test3.txt: ASCII text
[root@node2 Desktop]# 
[root@node2 Desktop]# ll test3.txt 
-rwxr--r--. 1 root root 563 Jan 21 19:45 test3.txt

4.4.转换到新文件

bash 复制代码
[root@node2 Desktop]# file test4*
test4.txt: ASCII text, with CRLF line terminators
[root@node2 Desktop]# 
[root@node2 Desktop]# dos2unix -n test4.txt test4_1.txt
dos2unix: converting file test4.txt to file test4_1.txt in Unix format ...
[root@node2 Desktop]# 
[root@node2 Desktop]# file test4*
test4_1.txt: ASCII text
test4.txt:   ASCII text, with CRLF line terminators

4.5.级联转换链接文件

bash 复制代码
[root@node2 Desktop]# ll test5*
lrwxrwxrwx. 1 root root   9 Jan 21 19:51 test5_ln1.txt -> test5.txt
-rwxr--r--. 1 root root 568 Jan 21 19:45 test5.txt
[root@node2 Desktop]# file test5*
test5_ln1.txt: symbolic link to 'test5.txt'
test5.txt:     ASCII text, with CRLF line terminators
[root@node2 Desktop]# 
[root@node2 Desktop]# dos2unix -F test5_ln1.txt  ## 如不加-F参数,会跳过文件不转换
dos2unix: converting file test5_ln1.txt to Unix format ...
[root@node2 Desktop]# 
[root@node2 Desktop]# file test5*
test5_ln1.txt: symbolic link to 'test5.txt'
test5.txt:     ASCII text
相关推荐
电鱼智能的电小鱼6 分钟前
产线视觉检测设备技术方案:基于EFISH-SCB-RK3588/SAIL-RK3588的国产化替代赛扬N100/N150全场景技术解析
linux·人工智能·嵌入式硬件·计算机视觉·视觉检测·实时音视频
子非衣21 分钟前
Ubuntu系统挂载磁盘并配置开机自动挂载
linux·运维·ubuntu
yyy00020031 分钟前
BGP选路实验
运维·网络
Lizhihao_1 小时前
TCP实现安全传输的核心机制 + TCP的报文讲解(全程图文讲解)
服务器·网络·网络协议·tcp/ip
风暴智能1 小时前
问题处理——在ROS2(humble)+Gazebo+rqt下,无法显示仿真无人机的相机图像
linux·无人机
唐志远19971 小时前
VMware虚拟机 安装 CentOS 7
linux·运维·centos
酷炫码神2 小时前
C#数据类型
java·服务器·c#
众乐乐_20082 小时前
Uniapp中小程序调用腾讯地图(获取定位地址)
linux·服务器·apache
power 雀儿2 小时前
Linux的进程管理和用户管理
linux·运维·服务器
.小墨迹2 小时前
Apollo学习——键盘控制速度
linux·开发语言·c++·python·学习·计算机外设