Convert from dos to linux

How to Convert Text Files between Unix and DOS (Windows) Formats

As a Linux administrator, you may have noticed some requests from developers to convert files from DOS format to Unix format, and vice versa.

This is because these files were created on a Windows system and copied to a Linux system for some reason.

It’s harmless, but some applications on the Linux system may not understand these new line of characters, so you need to convert them before using it.

DOS text files comes with carriage return (CR or \r) and line feed (LF or \n) pairs as their newline characters, whereas Unix text files only have line feed as their newline character.

There are many ways you can convert a DOS text file to a Unix format.

But I recommend using a special utility called dos2unix / unix2dos to convert text files between DOS and Unix formats.

  • dos2unix: To convert a text files from the DOS format to the Unix format.
  • unix2dos: To convert a text files from the Unix format to the DOS format.
  • tr, awk and sed Command: These can be used for the same purpose

You can easily identify whether the file is DOS format or Unix format using the od (octal dump) command as shown below.

# od -bc windows.txt 0000000 125 156 151 170 040 151 163 040 141 040 146 162 145 145 040 157 U n i x i s a f r e e o 0000020 160 145 156 163 157 165 162 143 145 040 157 160 145 162 141 164 p e n s o u r c e o p e r a t 0000040 151 156 147 040 163 171 163 164 145 155 015 012 123 165 160 145 i n g s y s t e m \r \n S u p e 0000060 162 040 143 157 155 160 165 164 145 162 163 040 141 162 145 040 r c o m p u t e r s a r e 0000100 162 165 156 156 151 156 147 040 157 156 040 125 116 111 130 015 r u n n i n g o n U N I X \r 0000120 012 071 065 045 040 157 146 040 167 145 142 163 151 164 145 163 \n 9 5 % o f w e b s i t e s 0000140 040 141 162 145 040 162 165 156 156 151 156 147 040 157 156 040 a r e r u n n i n g o n 0000160 114 151 156 165 170 040 117 123 015 012 101 156 171 164 150 151 L i n u x O S \r \n A n y t h i 0000200 156 147 040 143 141 156 040 142 145 040 144 157 156 145 040 157 n g c a n b e d o n e o 0000220 156 040 114 151 156 165 170 015 012 n L i n u x \r \n 0000231

The above output clearly shows that this is a DOS format file because it contains the escape sequence \r\n .

Читайте также:  Linux shared lib not found

At the same time, when you print the file output on your terminal you will get the output below.

# cat windows.txt Unix is a free opensource operating system Super computers are running on UNIX 95% of websites are running on Linux OS Anything can be done on Linux

How to Install dos2unix on Linux

dos2unix can be easily installed from the distribution official repository.

For RHEL/CentOS 6/7 systems, use the yum command to install dos2unix.

$ sudo yum install -y dos2unix

For RHEL/CentOS 8 and Fedora systems, use the dnf command to install dos2unix.

$ sudo yum install -y dos2unix

For Debian based systems, use the apt command or apt-get command to install dos2unix.

$ sudo apt-get update $ sudo apt-get install dos2unix

For openSUSE systems, use the zypper command to install dos2unix.

$ sudo zypper install -y dos2unix

1) How to Convert DOS file to UNIX format

The following command converts the “windows.txt” file from DOS to Unix format.

The modification of this file is to remove the “\r” from each line of the file.

# dos2unix windows.txt dos2unix: converting file windows.txt to Unix format …
# cat windows.txt 0000000 125 156 151 170 040 151 163 040 141 040 146 162 145 145 040 157 U n i x i s a f r e e o 0000020 160 145 156 163 157 165 162 143 145 040 157 160 145 162 141 164 p e n s o u r c e o p e r a t 0000040 151 156 147 040 163 171 163 164 145 155 012 123 165 160 145 162 i n g s y s t e m \n S u p e r 0000060 040 143 157 155 160 165 164 145 162 163 040 141 162 145 040 162 c o m p u t e r s a r e r 0000100 165 156 156 151 156 147 040 157 156 040 125 116 111 130 012 071 u n n i n g o n U N I X \n 9 0000120 065 045 040 157 146 040 167 145 142 163 151 164 145 163 040 141 5 % o f w e b s i t e s a 0000140 162 145 040 162 165 156 156 151 156 147 040 157 156 040 114 151 r e r u n n i n g o n L i 0000160 156 165 170 040 117 123 012 101 156 171 164 150 151 156 147 040 n u x O S \n A n y t h i n g 0000200 143 141 156 040 142 145 040 144 157 156 145 040 157 156 040 114 c a n b e d o n e o n L 0000220 151 156 165 170 012 i n u x \n 0000225

The above command will overwrite the original file.

Use the following command if you want to keep the original file. This will save the converted output as a new file.

# dos2unix -n windows.txt unix.txt dos2unix: converting file windows.txt to file unix.txt in Unix format …

1a) How to Convert DOS file to UNIX format Using tr Command

As discussed at the beginning of the article, you can use the tr command to convert the DOS file to Unix format as shown below.

Syntax: tr -d '\r' < source_file >output_file

The below tr command converts the “windows.txt” DOS file to Unix format file “unix.txt”.

Читайте также:  Альт линукс установка драйвера видеокарты

Make a note: You can’t use the tr command to convert a file from Unix format to Windows (DOS).

1b) How to Convert DOS file to UNIX format Using awk Command

Use the following awk command format to convert a DOS file to a Unix format.

Syntax: awk '< sub("\r$", ""); print >' source_file.txt > output_file.txt

The below awk command converts the “windows.txt” DOS file to Unix format file “unix.txt”.

2) How to Convert UNIX file to DOS format

When you convert a file from UNIX to DOS format, it will add a carriage return (CR or \r) in each of the line.

# unix2dos unix.txt unix2dos: converting file unix.txt to DOS format …

This command will keep the original file.

# unix2dos -n unix.txt windows.txt unix2dos: converting file unix.txt to file windows.txt in DOS format …

2a) How to Convert UNIX file to DOS format Using awk Command

Use the following awk command format to convert UNIX file to DOS format.

Syntax: awk 'sub("$", "\r")' source_file.txt > output_file.txt

The below awk command converts the “unix.txt” file to the DOS format file “windows.txt”.

# awk 'sub("$", "\r")' unix.txt > windows.txt

Источник

DOS to Unix: Commands and Examples

Files created in DOS/Windows use carriage return (\r) and line feed (\n) for line endings. However, files in Unix/Linux solely use line feed.

Therefore, when transferring a file from one system to another, make sure to convert the files.

In this tutorial, you will learn how to transfer files from DOS to Unix and vice-versa.

Guide on how to transfer DOS to UNIX files.

Converting Files on Linux

There are several ways you can transfer files to use the appropriate line endings. Convert the files using the:

  • dos2unix command
  • unix2dos command
  • sed command (see Linux sed command tutorial)
  • tr command
  • Vi/Vim text editor (see Vim commands cheat sheet)
  • perl one-liner command

Option 1: Converting DOS to UNIX with dos2unix Command

The simplest way to convert line breaks in a text file is to use the dos2unix tool.

Install the tool by running the command:

sudo apt install dos2unix
sudo dnf install dos2unix

The command to install dos2unix tool on Linux systems.

If you download a file created in DOS/Windows onto your Linux system, you can convert it using the dos2unix command:

The command converts the file without saving it in the original format. If you want to save the original file, add the -b attribute before the file name. Doing so creates a backup file under the same name and the .bak extension.

Option 2: Converting UNIX to DOS using the unix2dos Command

To convert a file created on a Linux system into the DOS format, run the command:

Alternatively, add the -b attribute to save the original file as backup:

Option 3: Using the sed Command

You can also use the sed (stream editor) command to remove the carriage return line endings. The command syntax to do so is:

sed 's/^M$//' [original_file_name]>[converted_file_name]

Instead of just typing ^M , press Ctrl+V followed by Ctrl+M to enter the carriage return symbol. When using the sed command, specify the name of the DOS file [original_file_name] and how you want to name the converted file [converted_file_name] .

Читайте также:  Google chrome x64 linux

To change the file format from Unix to DOS, use the command:

sed 's/$/^M/' [original_file_name]>[converted_file_name]

Note: Learn more about sed by referring to our article How to Use Sed to Find and Replace a String in a File.

Option 4: Using the tr Command

Another way to convert a file into the Unix format is to remove \r line endings with the tr command. The tr command is a command line utility for translating or deleting characters.

Use the command in the following format:

tr -d '\r' < [original_file_name]>[converted_file_name]

Option 5: Using the Vim Text Editor

You can also remove carriage return line endings from files in DOS format using the Vi/Vim text editor.
Open the file in Vi/Vim:

Then press : and type in the following Vi/Vim command (making sure you type Ctrl+V then Ctrl+m instead of ^m ):

Remove the carriage return line ending using the vi text editor.

Option 6: Using a Perl One Liner

Lastly, you can use a Perl one-liner command to delete all \r line endings. Pearl on-liners are scripts that fit in a single line of code.

To replace all carriage return and line feed endings with just line feeds:

1. Open the file in the Vi/Vim text editor.

2. Press : to prompt the command line.

3. Type in the following command and press Enter:

perl -pi -e 's/\r\n/\n/g' [file_name]

Delete carriage return endings using a Perl one-liner command.

You should see the the changes in the file right away.

Note: Need help finding files on your Linux system? Take a look at how to use the find command or how to show hidden files in Linux.

Example: Converting a Sample File from DOS to Unix Format

Let’s say you downloaded a file named sample-dos-file.

A sample file created in Windows.

By opening the file using the Vim/Vi text editor, you would see that after each line ending there is ^M (a carriage return).

Opening a DOS file in Vi to see carrier return symbols at line endigns.

Another way to see the file uses both carriage return and line feed for line endings is to view the file in octal values.

The output displays the content of the file with its octal values, as in the image below. You can see that each line end is marked with the octal values 015 (\r) and 012 (\n).

See end of line marks in octal values.

Now, to convert the file and remove all carriage return endings, you would run the command:

Convert a dos file into the Unix format.

Open the same file in Vim/Vi. It doesn’t include any ^M symbols signaling the line ending.

Open a file in the Vi text editor.

To confirm the file is in Unix format, you can also open view the content in octal values. The output should display just the 012 values for \n.

Display Unix file content in octal values.

This article showed six ways to convert a DOS file into a Unix format and vice versa.

The simplest and recommended way is using the dos2unix command. If you cannot utilize dos2unix , you have five other options to choose from.

Источник

Оцените статью
Adblock
detector