DOS to Unix: Commands and Examples

Introduction

DOS (Disk Operating System) and Unix are two of the most popular operating systems used in the world today. DOS is a command-line based operating system developed by Microsoft, while Unix is a multi-user, multitasking operating system developed by AT&T Bell Labs. Although both operating systems have their own unique features and advantages, they also share many similarities. This article will provide an introduction to DOS and Unix commands and examples of how to use them. It will also discuss the differences between the two operating systems and how they can be used together. Finally, it will provide some tips on how to get the most out of both operating systems.

DOS to Unix: Commands and Examples

DOS to Unix commands:

1. Copy: cp
Example: cp C:\myfile.txt /home/user/myfile.txt

2. Move: mv
Example: mv C:\myfile.txt /home/user/myfile.txt

3. Delete: rm
Example: rm /home/user/myfile.txt

4. Change Directory: cd
Example: cd /home/user

5. List Directory Contents: ls
Example: ls /home/user
[ad_1]

Introduction

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:

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

or:

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:

dos2unix [file_name]

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.

dos2unix -b [file_name]

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:

unix2dos [file_name]

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

unix2dos -b [file_name]

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].

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

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

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:

vi [file_name]

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

:%s/^ m //g
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.

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.

To do so, run the command:

od -bc sample-dos-file.txt

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:

dos2unix sample-dos-file
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.

Conclusion

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.

[ad_2]

DOS to Unix: Commands and Examples

The transition from DOS to Unix can be a difficult one, especially if you’re unfamiliar with the commands and syntax of the Unix operating system. To help make the transition easier, here are some of the most commonly used DOS commands and their Unix equivalents.

Directory Navigation

In DOS, the cd command is used to change directories. In Unix, the cd command is also used, but it is followed by a directory path. For example, to change to the /home/user directory, you would type cd /home/user.

Listing Files and Directories

In DOS, the dir command is used to list the contents of a directory. In Unix, the ls command is used instead. To list the contents of the current directory, you would type ls. To list the contents of a different directory, you would type ls directory_name.

Creating Directories

In DOS, the md command is used to create a new directory. In Unix, the mkdir command is used instead. To create a new directory called my_directory, you would type mkdir my_directory.

Deleting Files and Directories

In DOS, the del command is used to delete files. In Unix, the rm command is used instead. To delete a file called my_file.txt, you would type rm my_file.txt. To delete a directory and all of its contents, you would type rm -r directory_name.

Copying Files and Directories

In DOS, the copy command is used to copy files and directories. In Unix, the cp command is used instead. To copy a file called my_file.txt to a new location, you would type cp my_file.txt new_location. To copy a directory and all of its contents, you would type cp -r directory_name new_location.

Renaming Files and Directories

In DOS, the ren command is used to rename files and directories. In Unix, the mv command is used instead. To rename a file called my_file.txt to new_file.txt, you would type mv my_file.txt new_file.txt. To rename a directory, you would type mv old_directory_name new_directory_name.

Searching for Files and Directories

In DOS, the find command is used to search for files and directories. In Unix, the find command is also used, but it is followed by a directory path. For example, to search for a file called my_file.txt in the /home/user directory, you would type find /home/user -name my_file.txt.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
Jassweb, Rai Chak, Punjab, India. 143518