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…
This article requires an open terminal, like oh so many other articles. If you don’t know how to open the terminal, just press
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:
sudo apt install dos2unix
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:
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.
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.