- How to Convert a Windows File to a UNIX File
- Ways to Convert Windows File to Unix Format in Linux
- Convert Windows File to Unix Using dos2unix Command
- Using AWK Command – Convert Windows File to Unix
- Using tr Command – Convert Windows File to Unix
- How to convert some files from dos format to unix
- 3 Answers 3
- How to Use dos2unix Command on Linux
- Syntax
- Use dos2unix Command on Linux
- 1. Update the System
- 2. Install the dos2unix Command
- 3. Convert Files Using dos2unix Command
- 4. Conversion Modes in dos2unix Command
- 4. Recursive Conversion of Multiple Files
- 5. Dos2Unix Options
- 6. Convert Current File and Save in New File
- 7. Keep the Timestamp of the Converted File
How to Convert a Windows File to a UNIX File
You may ask yourself, what is the purpose of this article? Why convert a Windows file to adapt to a UNIX environment like Linux? Isn’t Linux all-powerful? The exceptional capabilities of the Linux operating system do not spare it from incompatible displays of files transferred from other computing platforms.
Just because you can open a file on a Linux environment does not imply that you have full control over how the file’s texts should be displayed.
You will encounter instances where a file’s texts or words are jammed together on a single giant line. In other instances, the same file texts’ displays might not have line breaks or sentence spacing.
A common attribute of raw Windows files opened in UNIX systems like Linux is the unavoidable end-of-line display of ^M or Ctrl-M characters.
This article guide seeks to achieve one objective; the conversion of a Windows File to a UNIX file without changing the format of the resulting file.
Ways to Convert Windows File to Unix Format in Linux
We can achieve the objective of our article through several methods. These methods allow us to convert a Windows file to a UNIX file and still retain the original format of the Windows file.
Convert Windows File to Unix Using dos2unix Command
Depending on your Linux operating system distribution, you can install the dos2unix command-line tool from one of the following commands:
$ sudo apt-get install dos2unix [On Debian, Ubuntu and Mint] $ sudo yum install dos2unix [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/dos2unix [On Gentoo Linux] $ sudo pacman -S dos2unix [On Arch Linux] $ sudo zypper install dos2unix [On OpenSUSE]
The command syntax for using the dos2unix tool is as follows:
$ dos2unix Your_Windows_File Final_Unix_File
So if you have a sample file created on a Windows computing system and want to open it on a Linux computing system without compromising its format, you would use the following command.
$ dos2unix windows_readme.txt unix_readme.txt
Before we run the above command, we need to create a blank unix_readme.txt file that will accommodate the converted file.
$ touch unix_readme.txt $ dos2unix windows_readme.txt unix_readme.txt
As per the screen capture, your converted Windows file should comfortably adapt to any Unix environment.
Using AWK Command – Convert Windows File to Unix
The awk command is pre-installed on all modern full-fledged UNIX computing systems like Linux. To convert our sample Windows file, we would implement the awk command in the following manner:
$ awk '< sub("\r$", ""); print >' windows_readme.txt > new_unix_readme.txt
As you have noted, with the awk command, we don’t need a pre-existing blank Linux file to accommodate the converted Windows file. The command creates and populates the Unix file version of the Windows file on its own.
Using tr Command – Convert Windows File to Unix
If most of the Windows files you open on your Linux environment have unnecessary Ctrl-Z and carriage return characters, then you will appreciate what the tr command has to offer.
Supposing our sample Windows file is a victim of such characters, removing them will require implementing the following command:
$ tr -d '' < windows_readme.txt >polished_unix_readme.txt
The inbuilt nature of the tr command also generates the resulting UNIX file without the need for its pre-creation.
The flexibility of the three discussed approaches to converting any editable Windows file to UNIX file format should save you from the headaches of having to manually edit your downloaded or transferred Windows files to remove unwanted characters and spaces while on a Linux environment.
How to convert some files from dos format to unix
I know how to change file format from dos to unix by use dos2unix , but how can I change ALL the files will under a directory tree. Can dos2unix change files recursively? for example, I have some files like following:
TOPDIR | +-----dir1 | | | +---file1,file2, file3 | +-----dir2 | +---file4,file5
You should start to play around with unix commands. There is no other way to learn how to use the power they offer. You have to try yourself and explore and conquer that environment step by step. Do it, it is worth it.
Regarding that, it’s always helpful to read the manpage or $CMD —help , since some programs (e.g. grep ) have a recursive flag -R that does this for you. Also if the count of files is not too high (i.e. in the order of a few hundred) you can use the extended glob capabilities of shells like zsh to do dos2unix **/* .
3 Answers 3
better to do find /path -type -f -exec dos2unix ‘<>‘ \;
@Andrey Dmitriev, what is the different between: find /path -type -f -exec dos2unix ‘<>‘ \; and find /path -type f -exec dos2unix <> +
on debian (wheezy) i get find: Arguments to -type should contain only one letter so i had to use find /path/ -type f -exec dos2unix ‘<>‘ \;
find /path -name ‘*’ -type f -exec dos2unix <> \;
In case you need to apply dos2unix to some files only, makes the example more easy to understand, imo (e.g. you can write -name ‘*.c’ )
. Maybe the thinking behind this answer is that -name ‘*’ would exclude dot files ( .* )? Well, in any case, it doesn’t (it used to in some cases, but now it doesn’t — so don’t assume it does). For example, .foo files or files in .git directories would be processed. Those files would have to be excluded, e.g., find path \( -name «.*» -prune \) -o \( -type f -print \) (note: you couldn’t start this with find . (etc) or else it would exclude everything)
How to Use dos2unix Command on Linux
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:
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:
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:
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:
Press the “Enter” key, and you will see a similar output as this:
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:
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.
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.