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?

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
  • 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 6 '15 at 9:52
  • @A.B. Fixed. You are right. They changed input parameters. I did not use it for a long time. – Pilot6 Jul 6 '15 at 9:57
  • Anyway, good answer +1 – A.B. Jul 6 '15 at 9: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 6 '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 6 '15 at 11:51
6

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.

  • 1
    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 6 '15 at 9:49
  • @ByteCommander YES, but he wants to get it run on Ubuntu. – user284234 Jul 6 '15 at 9:50
  • 1
    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 6 '15 at 9:52
  • 2
    I think the OP is referring to the built-in program Notepad in Windows, not the third-party program Notepad++. If you suggest that Nodepad++ should be installed on Windows for this purpose, can you make it more explicit in your answer? As it stands right now, I think it is confusing. – Peter Mortensen Jul 6 '15 at 21:16
  • @PeterMortensen I read again his question, I do not think I misunderstood and my answer is one of the good solutions to his problem. – user284234 Jul 7 '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 ...]
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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.