0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert DOS to UNIX Line Endings

Use the Bash command sed -i 's/\r$//' filename to convert DOS line endings to UNIX line endings by removing carriage return characters.
📋

Examples

InputLine1\r\nLine2\r\n
OutputLine1\nLine2\n
InputHello World\r\nThis is a test\r\n
OutputHello World\nThis is a test\n
InputNo DOS line endings here\nJust UNIX\n
OutputNo DOS line endings here\nJust UNIX\n
🧠

How to Think About It

To convert DOS to UNIX line endings, you need to remove the carriage return character \r that appears before the newline \n in DOS files. The script should scan each line and delete the trailing \r if present, leaving only the UNIX newline \n.
📐

Algorithm

1
Get the filename as input.
2
Read the file line by line.
3
For each line, check if it ends with a carriage return character <code>\r</code>.
4
If yes, remove the <code>\r</code> character.
5
Save the modified lines back to the file or to a new file.
💻

Code

bash
#!/bin/bash

if [ $# -ne 1 ]; then
  echo "Usage: $0 filename"
  exit 1
fi

filename="$1"

# Remove carriage return characters at end of lines
sed -i 's/\r$//' "$filename"

echo "Converted DOS to UNIX line endings in $filename"
Output
Converted DOS to UNIX line endings in example.txt
🔍

Dry Run

Let's trace converting a file with DOS line endings 'Line1\r\nLine2\r\n' through the script.

1

Input file content

Line1\r\nLine2\r\n

2

sed command processes each line

For line 'Line1\r', sed removes '\r' at end, resulting in 'Line1'

3

Second line processed

For line 'Line2\r', sed removes '\r' at end, resulting in 'Line2'

4

File saved with UNIX line endings

Final content: 'Line1\nLine2\n'

StepLine BeforeLine After
1Line1\rLine1
2Line2\rLine2
💡

Why This Works

Step 1: Why remove \r characters?

DOS line endings use \r\n (carriage return + newline), but UNIX uses only \n. Removing \r converts the file to UNIX style.

Step 2: How sed works here

The sed command uses the pattern s/\r$// to find a carriage return at the end of each line and delete it.

Step 3: In-place editing

The -i option edits the file directly, so the changes replace the original content.

🔄

Alternative Approaches

Using dos2unix command
bash
dos2unix filename
This is a dedicated tool for this task and is very simple, but it may not be installed by default on all systems.
Using tr command
bash
tr -d '\r' < filename > newfile
This removes all carriage returns but writes to a new file, so you must handle file replacement manually.
Using awk
bash
awk '{ sub(/\r$/, ""); print }' filename > newfile
This removes trailing carriage returns and writes to a new file, useful if you want to keep the original file.

Complexity: O(n) time, O(1) space

Time Complexity

The script processes each line once, so time grows linearly with file size.

Space Complexity

The operation is done in-place with sed -i, so extra space is minimal.

Which Approach is Fastest?

Using sed -i or dos2unix is fastest for in-place conversion; tr and awk require extra file writes.

ApproachTimeSpaceBest For
sed -iO(n)O(1)Quick in-place edits
dos2unixO(n)O(1)Dedicated tool, easy use
trO(n)O(n)Simple removal, new file output
awkO(n)O(n)Flexible processing, new file output
💡
Always back up your file before running in-place conversion commands.
⚠️
Forgetting to use the -i option with sed causes output to print on screen instead of modifying the file.