Running a script created in Notepad (Windows) on Ubuntu
Solution 1
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
Solution 2
On Windows, you need to change the End of Line (EOL) format in Notepad++ to UNIX:
That way it will work on Ubuntu too.
Solution 3
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 ...]
Solution 4
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.
Related videos on Youtube
Albert
Updated on September 18, 2022Comments
-
Albert almost 2 years
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?
-
jfs almost 9 years
-
-
Byte Commander almost 9 yearsThat 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.
-
Admin almost 9 years@ByteCommander YES, but he wants to get it run on Ubuntu.
-
Byte Commander almost 9 yearsYes, 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.
-
A.B. almost 9 yearsYour command is wrong.
dos2unix winfile.txt linuxfile.txt
tries to convert both files. Use this commanddos2unix -n winfile.txt linuxfile.txt
-
Pilot6 almost 9 years@A.B. Fixed. You are right. They changed input parameters. I did not use it for a long time.
-
Albert almost 9 yearsThank 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.
-
Pilot6 almost 9 years@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 almost 9 years@Albert Added sed version to edit file in place.
-
Admin almost 9 years@PeterMortensen I read again his question, I do not think I misunderstood and my answer is one of the good solutions to his problem.
-
Sergiy Kolodyazhnyy almost 9 yearsAlso, sidenote:Window's files don't understand unix permissions.