Convert unix to dos linux

Bash script to convert all files of a given type from Unix to Dos Format

I need to be able to go back and forth between CentOS and Windows for C++ projects that I am developing on both systems. I want to be able to convert all files of a given type such as *.cpp, *.h or *.txt from Unix to Dos. I have a bash script that converts a single file, but I’d like to be able to type in either just the extension of the file type or *.FILETYPE to convert all the files in a given directory of that type. Here is the working script, what do I need to do to make it work for more than one file at a time. I’ve tried using a for loop but I get syntax errors.

#! /bin/sh # Convert a text based file from Unix Format to Dos Format # The Output file should be the name of the input file with # WF inserted before the file extention # # Examples # convert Unix format abc.txt to abcWF.txt Windows Format # convert Unix format abc.cpp to abcWF.cpp Windows Format echo "In u2w FileToConvert = $1" FileBase=$(basename $1) FileExtension="$" FileBase="$" OutputSuffix="WF.$FileExtension" OutputFile="$FileBase$OutputSuffix" commandment="unix2dos -n $1 $OutputFile" echo $commandment unix2dos -n $1 $OutputFile echo "diff -w $1 $OutputFile" diff -w "$1" "$OutputFile" exit 
$ bd2w TestResults.txt In u2w FileToConvert = TestResults.txt unix2dos -n TestResults.txt TestResultsWF.txt unix2dos: converting file TestResults.txt to file TestResultsWF.txt in DOS format . diff -w TestResults.txt TestResultsWF.txt 

Источник

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.

Table of Contents Hide

How to Convert Files on Linux?

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

Читайте также:  Alt linux nvidia settings

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:

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.

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:

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:

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.

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:

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

Читайте также:  Удаление mozilla firefox linux

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.

Источник

How to Use dos2unix Command on Linux

Install Telegram on Rocky Linux 9 2

The dos2unix command is essentially used for file conversion in Linux. There are certain differences between files that are created in Windows and Linux. Linux administrators often have to convert files between different OS. Hence, they use the dos2unix command.

In this article, you will learn how to use the dos2unix command on Linux. This article will also serve as a guide to various dos2unix conversion examples. So, let’s get started!

Syntax

The syntax for the command looks something like this:

dos2unix [options] [FILE . ] [-n INFILE OUTFILE . ]

The command takes two arguments: options and a filename.

Use dos2unix Command on Linux

The files created in Dos/Windows-based operating systems have different formats compared to Linux-based files. For instance, DOS files use \r and \n at the end of each line. On the other hand, Linux/Unix files use only \n escape sequences. Hence, it is essential to convert files when transferring them.

Let’s look at the guide on how to use this command on Linux.

1. Update the System

Firstly, update the system before installing the command. Open the terminal using the shortcut key “Ctrl + Alt + T”. Next, use the update command.

The output should look something like this:

use dos2unix command on Linux

2. Install the dos2unix Command

Secondly, install the dos2unix command. For this step, use the install command as shown below:

sudo apt install dos2unix

You should get a similar output:

use dos2unix command on Linux

Wait for the installation to complete.

3. Convert Files Using dos2unix Command

Now that the command is successfully installed, you can use this to convert the DOS files. For example, to convert a windows-based file without saving its original format, type the command given below:

However, if you want to retain the original file with the same name, use the -b option with the dos2unix command. Specifically, type:

use dos2unix command on Linux

4. Conversion Modes in dos2unix Command

There are various conversion modes concerning encoding types. For ASCII encoding, dos2unix has “ascii” mode, which is the default conversion mode. This converts UTF-8 files into ASCII mode.

However, special characters in windows-based files that do not have an equivalent character in ISO-8859-1 get converted into dots. Some examples of conversion modes are:

# Convert DOS-based files to Unix Latin-1: dos2unix -iso -n myFile.txt outFile.txt # Convert from DOS-based CP850 to Unix Latin-1: dos2unix -850 -n myFile.txt outFile.txt 

On the other hand, if you are unsure of the file mode, you can check it. For this step, you will use the vim editor. Type the command given below to open the file with vim editor:

If you do not have it installed, you can simply install it here. Next, in command mode, type the following command:

Читайте также:  Запустить qt creator linux

Press the “Enter” key, and you will see a similar output as this:

find file type for dos2unix command

Since this is a UNIX-based file, it is showing fileformat=unix. After that, exit the vim editor using the :q command. Another way to check the file format is to use the od command. Specifically, type:

4. Recursive Conversion of Multiple Files

In the dos2unix command, you can also perform recursive conversion of several files. In this case, the dos2unix command is used in combination with the find and xargs command. For this step, type:

find . -name *.txt | xargs dos2unix

5. Dos2Unix Options

You can use the dos2unix command with a variety of options. These options provide flexibility in modifying the output as needed. Let’s look at some of the options in detail.

To view help related to the dos2unix command, use the help option. Specifically, type:

The output should look something like this:

dos2unix options

Alternatively, you can use the –help flag as well.

In addition, you can also check the version of the dos2unix command. Execute the command with the -v or –version flag.

dos2unix version

When performing a recursive conversion, you can skip binary files in the process. For this step, use the -s flag. For example:

On the other hand, you can force convert binary files as well. This will require you to use the -f option. For instance, type:

To avoid the warnings generated by the dos2unix command, suppress them using the quiet option. Execute the command given below:

You can also save the timestamps of your converted files. Since the conversion changes the creation date of the converted files, you can use the dos2unix command to retain the original creation date. Use the -k option with the dos2unix command. For example:

6. Convert Current File and Save in New File

Using the dos2unix command, you can save the converted file in a new file. For instance, you can convert myFile.txt and save the converted file in newFile.txt. You will use the -n option for this step. Type:

dos2unix -n myFile.txt newFile.txt

7. Keep the Timestamp of the Converted File

You can also save the converted file and keep the timestamp of the old file. This can be achieved by combining two options: -k and -n . For example:

dos2unix -k -n myFile.txt newFile.txt

And that’s a wrap! By following this guide, you can now comfortably convert files between DOS and UNIX operating systems. What we have covered in the article is just the top of the surface. To learn more about this command, visit their official website. You can also convert files from UNIX to DOS by using its sibling command unix2dos .

We hope you found this article to be helpful. If you have any handy examples on dos2unix, please let us know in the comments section, and we’ll try to answer them in the next articles.

If this guide helped you, please share it.

Sidrah is a staff writer at Distroid and has a Masters in Computer Science, with her key two focus areas being SQL and DevOps. She has written over 100 posts and specializes in all things Linux. She also has experience trying out various other distros, BASH scripting, Git, and software testing. LinkedIn GitHub

Leave a Reply

You must be logged in to post a comment.

Источник

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