Linux文字檔與WIN文字檔換行格式轉換命令

來源:互聯網
上載者:User

有 時在WIN下編輯好的指令檔上傳到LINUX伺服器中不能正常執行,開始誤認為是LINUX配置問題,後來發現,是WIN與LINUX隱藏檔時的換行 符標誌不同造成的。在DOS使用的分行符號為 ^M$,我們稱為CR與LF兩個符號。而在Linux中,則僅有LF ($) 這個分行符號。

可以用如下命令完成格式轉換:$dos2unix,$unix2dos。但這兩個命令在UBUNTU發行版本中不存在,可通過:

$sudo apt-get install tofrodos

命令安裝。之後,再次使用如下文所示的格式即可。

[root@linux ~]# dos2unix [-kn] file [newfile]

[root@linux ~]# unix2dos [-kn] file [newfile]

參數:

-k : 保留該檔案原來的mtime時間格式(不更新檔案上次內容經過修改的時間)

-n : 保留原來的舊檔案,將轉換後的內容輸出到新檔案,如:dos2unix -n old new

範例:

範例一:將提供的hosts檔案格式更新為dos格式。

[root@linux ~]# unix2dos -k hosts

unix2dos: converting file hosts to DOS format ...

# 此時hosts檔案的時間不會改變,但內容主要將分行符號修改成為DOS的CRLF了。

範例二:將範例一已經變成DOS格式的hosts改名為hosts.dos,並且轉換Linux

        格式到hosts.linux

[root@linux ~]# mv hosts hosts.dos

[root@linux ~]# dos2unix -k -n hosts.dos hosts.linux

dos2unix: converting file hosts.dos to file hosts.linux in UNIX format ...

[root@linux ~]# ll

-rw-r--r-- 1 root root       288 Aug 1 13:30 hosts.dos

-rw------- 1 root root      279 Aug 1 13:30 hosts.linux

# 由於DOS格式中多了CR字元,所以,檔案比較大。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.