14

I created a Notepad file on Windows and copied it to Ubuntu. The file contains some iptables rules. After making the file executable using chmod +x and executing it, it didn't work.

However, when I created a Ubuntu (gedit) file and copied the same contents from the Notepad file, made it executable and ran it, it worked.

What should I do to make the Notepad file run on Ubuntu?

Pilot6
  • 90,100
  • 91
  • 213
  • 324
Albert
  • 145

5 Answers5

20

Windows and Linux have different end-of-line symbols.

You can install the dos2unix utility that fixes it:

sudo apt-get install dos2unix

Run it this way:

dos2unix -n winfile.txt linuxfile.txt

There is also the unix2dos utility.

The Windows-to-Linux conversion can also be done without installing any special software by

 tr -d '\r' < winfile.txt > linuxfile.txt

Note: Input and output files must be different.

A sed version will edit the file "in place":

 sed  -i 's/\r//g' file.txt

Or write to another file:

 sed 's/\r//g' winfile.txt > linuxfile.txt
Pilot6
  • 90,100
  • 91
  • 213
  • 324
  • Your command is wrong. dos2unix winfile.txt linuxfile.txt tries to convert both files. Use this command dos2unix -n winfile.txt linuxfile.txt – A.B. Jul 06 '15 at 09:52
  • @A.B. Fixed. You are right. They changed input parameters. I did not use it for a long time. – Pilot6 Jul 06 '15 at 09:57
  • Anyway, good answer +1 – A.B. Jul 06 '15 at 09:57
  • Thank you all very much. Since I don't have Notepad++ and I didn't want to install a new package (dos2unix), I preferred the "tr" command. I tried it and it worked. But please note that the source and destination files must be different. – Albert Jul 06 '15 at 11:47
  • @Albert Added to the answer. It can be done another way to keep the file name, but it is not worth the effort IMHO. And it is better to have different names anyway not to mix files up. – Pilot6 Jul 06 '15 at 11:51
  • @Albert Added sed version to edit file in place. – Pilot6 Jul 06 '15 at 12:07
  • Also, sidenote:Window's files don't understand unix permissions. – Sergiy Kolodyazhnyy Aug 12 '15 at 21:52
5

On Windows, you need to change the End of Line (EOL) format in Notepad++ to UNIX:

enter image description here

That way it will work on Ubuntu too.

  • 2
    That way it will work only on Ubuntu/Linux. While special advanced editors like Notepad++ might recognize the line ending type and display it correctly anyway, normal Windows notepad etc. won't. – Byte Commander Jul 06 '15 at 09:49
  • @ByteCommander YES, but he wants to get it run on Ubuntu. –  Jul 06 '15 at 09:50
  • 2
    Yes, of course. I know and upvoted therefore. I just wanted to make this fact clear, so that nobody will be surprised when the file now looks strange in normal notepad. – Byte Commander Jul 06 '15 at 09:52
  • @PeterMortensen I read again his question, I do not think I misunderstood and my answer is one of the good solutions to his problem. –  Jul 07 '15 at 10:48
2

Windows uses CR+LF for line breaks. In Linux/Unix you need LF. Therefore you have to replace CR+LF into LF in your script:

Install dos2unix

sudo apt-get install dos2unix

And correct your script via

dos2unix <your_script_file>

or via

dos2unix -n <your_script_file> <out_file>

if you need a different output file

More informations here


from man dos2unix

NAME
       dos2unix - DOS/Mac to Unix and vice versa text file format converter

SYNOPSIS
           dos2unix [options] [FILE ...] [-n INFILE OUTFILE ...]
           unix2dos [options] [FILE ...] [-n INFILE OUTFILE ...]
A.B.
  • 90,397
1

I use cygwin on Windows.
Open the file with vi. Then enter

:set ff=unix<enter> followed by 
:wq<enter> 

will save the file with Unix end of line characters.

zx485
  • 2,426
0

You can use Vim in Ex mode:

ex -bsc '%s/\r//|x' file
  1. % select all lines

  2. s substitute

  3. \r carriage return

  4. x save and close

Zombo
  • 1