当前位置: 代码迷 >> 综合 >> Linux命令:dos2unix和unix2dos
  详细解决方案

Linux命令:dos2unix和unix2dos

热度:59   发布时间:2024-01-24 20:15:31.0

dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令。Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2unix命令其实就是将文件中的\r\n 转换为\n。

而unix2dos则是和dos2unix互为孪生的一个命令,它是将Linux&Unix格式文件转换为Windows格式文件的命令。

 

实例:

1、dos2unix filename 将Windows格式文本转换为Unix&Linux格式文件

2、dos2unix 可以一次转换多个文件:dos2unix filename1 filename2 filename3

3、默认情况下会在源文件上进行转换,如果需要保留源文件,那么可以使用参数-n dos2unix -n oldfilename newfilename

5、保持文件时间戳不变:

1: [root@DB-Server myscript]# ls -lrt dosfile 
2: -rw-r--r-- 1 root root 67 Dec 26 11:46 dosfile
3: [root@DB-Server myscript]# dos2unix dosfile 
4: dos2unix: converting file dosfile to UNIX format ...
5: [root@DB-Server myscript]# ls -lrt dosfile 
6: -rw-r--r-- 1 root root 65 Dec 26 11:58 dosfile
7: [root@DB-Server myscript]# dos2unix -k dosfile 
8: dos2unix: converting file dosfile to UNIX format ...
9: [root@DB-Server myscript]# ls -lrt dosfile 
10: -rw-r--r-- 1 root root 65 Dec 26 11:58 dosfile

 

6:静默模式格式化文件

 1: [root@DB-Server myscript]# unix2dos -q dosfile 
 2:  
 3: [root@DB-Server myscript]#