Let’s Talk About Using dos2unix

Today, we’re going to talk about using dos2unix, a tool for converting Windows text files to Unix (Linux) formatted text files. The command isn’t very difficult to use, so this article is more about making you aware of the existence and purpose of said dos2unix. It should be a remarkably short article.

First, we have to go back in time…

Once upon a time, when you reached the end of the line with text, you had to instruct the hardware to start a new line. These were the old days of things like teletype machines and dumb terminals. They needed to be told when to begin a new line.

Well, that still exists in software today. The thing is, if a file was created on Windows it will have two characters denoting when a new line should begin while a file created on Linux will only have one character indicating when a new line should begin. Yay for compatibility issues!

Anyhow… This is why you will sometimes get errors with text files. For example, writing a shell script on Windows and then trying to use it on Linux may result in end of line errors. Windows uses CR (Carriage Return) and LF (Line Feed, you may know it as /n) while Linux (and Unix) just uses LF. This, of course, can cause some confusion for the operating system.

This is where dos2unix comes into play. This is why you’re getting an article about using dos2unix… So, with that in mind…

Using dos2unix:

This article requires an open terminal, like oh so many other articles. If you don’t know how to open the terminal, just press CTRL + ALT + T and your default terminal should open. Tada!

Now, you’ll need to install dos2unix. It probably isn’t installed by default. Just use your package manager’s standard install command and I’m like 99% certain it’s a package available to you by default. For example, in Debian, Ubuntu, and Ubuntu derivatives and flavors, it’s just a simple apt command:

Once you have dos2unix installed, you can convert Windows-created text files to Unix (and thus Linux) formatted text files by fixing the way they end a line and start a new line. That’s really easy. It’s just:

Yup…

Well, I suppose you could also do /path/to/filename.txt if you needed, or you can just navigate to the directory and use the command above. It really is that easy.

There’s more to the dos2unix application, but that’s about all you’re really going to need to know under normal circumstances. It really is that simple. Do check the man page (man dos2unix) for other options, but you’ll see that’s about all you’re gonna need.

Closure:

Like I said, this article is really about making you aware of the problem and solution, so that you can start using dos2unix if and when you need it. It turns out that dos2unix is more than just a contributing forum member on Linux.org, it’s actually a useful application for dealing with Windows files on a Linux box!

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe to Newsletter!
Get notified when new articles are published!
We promise to never share your email!
icon

Author: KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

2 thoughts on “Let’s Talk About Using dos2unix”

  1. Good article. I don’t know how many times I’ve been burned by trying to cut and paste something out of an outlook email like a bash script only to spend time figuring out why it wouldn’t work due to special characters. Unfortunately, I think in those cases this would not have helped. I would have to retype or use sed to strip out things….

    1. Hmm… In those cases, you should be able to paste it to a file and then run dos2unix on that file and then go ahead and use it…

      I think… I mean, that seems logical, like it should work.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.