dos2unix and unix2dos Commands Usage

dos2unix

Sometimes, we need to transfer files between Unix and Windows systems. In Windows and Dos files, the line break is represented using two characters: the first is the carriage return (\r) (CR) and the second is the Line feed (\n) (LF). On the other hand, in Linux/Unix distributions the end of the line is indicated by using only one character that is a Line Feed (LF). However, this difference may cause issues, like code not compiling, scripts not working, text formating looking off.

Using the popular commands dos2unix and unix2dos, we can avoid the hidden character problems.

Objectives

The main objective of this article is to explain the workings of dos2unix and unix2dos commands. We will also learn how to transfer a file from Dos to Unix and vice versa using these two commands. We explain both commands with the help of a few examples.

How to Convert Files on Linux?

Using the following two most commonly used Linux commands, you can convert the file on a Linux system:

  1. unix2dos command
  2. dos2unix command

Let’s explain the use of each command with the help of some examples.

dos2unix command – Converts a DOS/Windows text file to Unix format

The dos2unix utility converts the DOS plain text files to a Unix format. This tool can be installed on Ubuntu/Debian systems by running the command:

 sudo apt install dos2unix

dos2unix installation

To install this tool on Fedora/CentOS 8 distributions, type the below-mentioned command:

 sudo dnf install dos2unix

Syntax of dos2unix command

Usage of the dos2unix command:

 dos2unix [options] [dos-text-file]

To convert a file from DOS format to Unix is quite simple using the dos2unix utility. You need a text file created on a Dos/Windows system. If you download this file on your system then, by running the dos2unix command you can easily convert this text file into the Unix format as follows:

 dos2unix [file-name]

The above command converts the file without saving it in the original format. But, if you need to save the original file, use the -b option or attribute before the name of the file. This action creates a backup of the original file under the same name with the .bak extension.

 dos2unix -b [file-name]

You can also use some additional options based on your specific situation.

Example: File conversion from DOS to Unix format

For example, we downloaded a file named dos-sample-file.txt on the Linux system. Now, display the file content using the vim text editor. You will observe, at the end of each line ^M symbols are added. That means this file is in Dos format.

dos sample file on

You can also use the below-given command to verify that this file is in DOS format:

 od -bc Dos-sample-file.txt

view encoded dos file

Now, to convert this text file from DOS to Unix format, use the dos2unix command as follows:

 sudo dos2unix Dos-sample-file.txt

conversion

To display the help related to the dos2unix command, run the below-given command:

 dos2unix --help

The following output shows on the terminal including all additional options:

dos2unix help

unix2dos command – Converts a Unix file to DOS/Windows format

The unix2dos utility converts the Linux/Unix plain text files in the DOS and window formats. This command is very useful while you need to transfer or export a file from Linux to Windows operating system.

Syntax of unix2dos command

The following syntax is used to convert a file which is created on a Linux system into the DOS format via the unix2dos command:

 unix2dos [options] [File-name]

Use the-b option to save the original Unix file as a backup on your system.

 unix2dos -b [File-name]

Example: File conversion from Unix to DOS format

For example, we want to convert a Unix text file named unix-sample-file.txt to a DOS file format. In this case, use the above command in the following way:

 unix2dos -b unix-sample-file.txt

unix2dos conversion

The following output displays on your terminal screen:

unix2dos encoded file

To confirm the conversion of the above text file into the DOS format, open the text file in the vim text editor as follows:

unix2dos encoded file

You noticed here the ^M has been removed at the end of each line. Now, run the following command for confirmation that the unix-sample-file.txt is now in the DOS format:

 od -bc unix-sample-file.txt

converted unix file

Useful options for unix2dos and dos2unix commands

The following options you can use along with dos2unix and unix2dos commands to convert from DOS to Unix format and vice versa.

-h (--help) – display help and multiple options and attributes.
-k (--keepdate) – This option Keeps the date stamp of the output file the same as the input file.
-l (--newline) – Add an additional newline.
-q (--quiet) – Quiet mode. Suppress all warnings and messages.
-V (--version) – Prints version details.
-f (--force) – This option forces conversion of binary files.
-s (--safe) – by default, used to Skip binary files.
-F (--follow-symlink) – convert the targets and follow symbolic links and.
-S (--skip-symlink) – used to leave the symbolic links and targets unchanged (default).

Conclusion

We explored the uses of dos2unix and unix2dos commands on a Linux system. We also mentioned different options attributes that will help you to solve your problem.

 

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like