Session and utilities

The dos2unix command

Converts Windows line endings (CRLF) to Unix format (LF) and removes the BOM if there is one. It is essential to run shell scripts or to read files created on Windows inside Linux.

Syntax

dos2unix [opciones] archivo...Try it in the terminal →

Most useful options

-n ENTRADA SALIDAWrites the result to a new file and leaves the original untouched
-OSends the result to standard output. The original file is not touched
-qSuppresses the warnings, for example the ones about skipped binary files

Copy-ready examples

$ dos2unix informe.txtconverts the file in place and shows what it did.

Example output

dos2unix: converting file informe.txt to Unix format...
$ dos2unix -n entrada.txt salida.txtcreates salida.txt in Unix format and leaves entrada.txt the same.

Example output

dos2unix: converting file entrada.txt to file salida.txt in Unix format...
$ dos2unix -O datos.csv > datos_lf.csvconverts while redirecting the result to another file.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
dos2unix: nonexist: No such file or directoryThe file does not exist or the name is misspelled.Check the path with ls and run the command again.
dos2unix: Skipping carpeta, not a regular file.You passed a directory. dos2unix only converts regular files.Name the files one by one or loop over the directory.
dos2unix: Skipping binary file bin.datThe file contains null bytes and dos2unix treats it as binary. It skips it to avoid damaging it.If it really is text, run it again with -f. If not, leave it as it is.

Typical mistakes

  • By default it overwrites the original with no backup. Before converting important files, use -n or copy the file.
  • Running it on a file that already has LF does not spoil it: it comes out the same.
  • Word or Excel documents are not plain text. Do not pass them through dos2unix.
  • A BOM at the start is removed by default. Use -b to keep it, for example in a CSV that Excel reads correctly thanks to it.

Differences from similar commands

  • unix2dos: does the opposite: it converts LF to CRLF.
  • tr: with -d and the carriage return character it removes CR characters, but it does not detect binary files or handle the BOM.
  • file: tells you which line endings a file has before you convert it.

Related commands

FAQ

Why do I see ^M at the end of every line?

It is the Windows carriage return (CR). Run dos2unix on the file to remove it.

Do I need dos2unix if the file comes from Windows?

Yes, if you are going to run it. A script with CRLF fails because the interpreter name carries the carriage return.

How do I convert several files at once?

Pass several names or use a wildcard, for example dos2unix *.txt. Remember that it overwrites the originals.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course