- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?
When we transfer files between Windows and Unix systems, often we come across with issue relating to the end of line character. This is because the EOL character in windows is not recognized as a EOL character in Unix. SO to fix this issue when a file is transferred from Windows to Unix we need to follow one of the below methods.
The dos2unix command is used to convert the EOL character of windows platform to Unix platform. Most of the Unix system comes with this command pre-installed. Below we see how we can convert the file itself or save a copy of the file, when using this command.
# change the file itself. dos2unix file_name.txt # Save a copy of the file. dos2unix -n file_name.txt new_file_name.txt
When we transfer the file from windows to Unix platform and open to read it, the end of line character appears as ^M (called control-M). So we use sed command to replace the ^M characters. Here also we can save the edited file as a new file.
sed 's/^M$//' file_name.txt > new_file_name.txt
The tr command is used in translating or deleting the characters. In this case we will use it to delete the end of the line character in the file from Windows system, if it appears as â\râ in the unix system when opened.
tr -d '\r' file_name.txt > new_file_name.txt
After the above command executes successfully, we can successfully open the file in Unix system without showing the EO L values from Windows system.
Kickstart Your Career
Get certified by completing the courseGet Started