linux 操作系统下 dos2unix 命令介绍和使用案例
dos2unix 命令介绍
dos2unix 是一个在 Linux 操作系统中使用的命令行工具,主要用于将 DOS 格式的文本文件转换为 Unix 格式。DOS 格式的文本文件使用回车符(CR)和换行符(LF)作为换行标志,即 \r\n
,而 Unix 格式则仅使用换行符 \n
。因此,使用 dos2unix 可以解决在 Linux 系统中处理 Windows 格式文件时出现的换行符不匹配问题
命令安装部署
root@doris23:~# which dos2unix
root@doris23:~# dos2unix
Command 'dos2unix' not found, but can be installed with:
apt install dos2unix
root@doris23:~# apt install dos2nuix
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package dos2nuix
root@doris23:~# apt update
Hit:1 http://mirrors.aliyun.com/ubuntu focal InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu focal-security InRelease
Get:3 http://mirrors.aliyun.com/ubuntu focal-updates InRelease [128 kB]
Hit:4 http://mirrors.aliyun.com/ubuntu focal-backports InRelease
Ign:5 https://download.docker.com/linux/ubuntu jammy InRelease
Ign:5 https://download.docker.com/linux/ubuntu jammy InRelease
Hit:5 https://download.docker.com/linux/ubuntu jammy InRelease
Fetched 128 kB in 4s (30.9 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
15 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@doris23:~# apt install dos2unix
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
dos2unix
0 upgraded, 1 newly installed, 0 to remove and 15 not upgraded.
Need to get 374 kB of archives.
After this operation, 1,342 kB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu focal/universe amd64 dos2unix amd64 7.4.0-2 [374 kB]
Fetched 374 kB in 0s (1,079 kB/s)
Selecting previously unselected package dos2unix.
(Reading database ... 74417 files and directories currently installed.)
Preparing to unpack .../dos2unix_7.4.0-2_amd64.deb ...
Unpacking dos2unix (7.4.0-2) ...
Setting up dos2unix (7.4.0-2) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning linux images...
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
命令使用介绍
root@doris23:~# dos2unix --help
Usage: dos2unix [options] [file ...] [-n infile outfile ...]
--allow-chown allow file ownership change
-ascii convert only line breaks (default)
-iso conversion between DOS and ISO-8859-1 character set
-1252 use Windows code page 1252 (Western European)
-437 use DOS code page 437 (US) (default)
-850 use DOS code page 850 (Western European)
-860 use DOS code page 860 (Portuguese)
-863 use DOS code page 863 (French Canadian)
-865 use DOS code page 865 (Nordic)
-7 convert 8 bit characters to 7 bit space
-b, --keep-bom keep Byte Order Mark
-c, --convmode conversion mode
convmode ascii, 7bit, iso, mac, default to ascii
-f, --force force conversion of binary files
-h, --help display this help text
-i, --info[=FLAGS] display file information
file ... files to analyze
-k, --keepdate keep output file date
-L, --license display software license
-l, --newline add additional newline
-m, --add-bom add Byte Order Mark (default UTF-8)
-n, --newfile write to new file
infile original file in new-file mode
outfile output file in new-file mode
--no-allow-chown don't allow file ownership change (default)
-o, --oldfile write to old file (default)
file ... files to convert in old-file mode
-q, --quiet quiet mode, suppress all warnings
-r, --remove-bom remove Byte Order Mark (default)
-s, --safe skip binary files (default)
-u, --keep-utf16 keep UTF-16 encoding
-ul, --assume-utf16le assume that the input format is UTF-16LE
-ub, --assume-utf16be assume that the input format is UTF-16BE
-v, --verbose verbose operation
-F, --follow-symlink follow symbolic links and convert the targets
-R, --replace-symlink replace symbolic links with converted files
(original target files remain unchanged)
-S, --skip-symlink keep symbolic links and targets unchanged (default)
-V, --version display version number
命令语法
dos2unix 的基本语法如下:
bash
dos2unix [OPTIONS] file1 [file2 ...]
其中,OPTIONS
为可选参数,file1
, file2
等为要转换格式的文件名
常用选项
-k
:保持输出文件的日期不变。-q
:安静模式,不显示任何警告信息。-V
:查看版本信息。-c convmode
:指定转换模式(如 ASCII、7bit、ISO 等),默认为 ASCII。-o
:写入到源文件。-n
:将转换结果写入新文件,而不修改原文件
案例使用
root@doris23:~# ls
AI服务代码相关.txt snap
root@doris23:~# cat AI服务代码相关.txt
204.150 /data/longsz/anaconda3 64G
204.155 /data/longsz/anaconda3 85G
204.154 /data/weisy 524G
192.168.102.9
/data/tmp_git/code
#τ¼þ±¸·
/data/backup_env
keep_gitlab-bk-20231122.tgzroot@doris23:~# file AI服务代码相关.txt
AI服务代码相关.txt: ISO-8859 text, with CRLF line terminators
root@doris23:~# dos2unix AI服务代码相关.txt
dos2unix: converting file AI服务代码相关.txt to Unix format...
root@doris23:~# file AI服务代码相关.txt
AI服务代码相关.txt: ISO-8859 text
root@doris23:~# cat AI服务代码相关.txt
204.150 /data/longsz/anaconda3 64G
204.155 /data/longsz/anaconda3 85G
204.154 /data/weisy 524G
192.168.102.9
/data/tmp_git/code
#τ¼þ±¸·
/data/backup_env
keep_gitlab-bk-20231122.tgzroot@doris23:~#