Linux dos2unix Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the dos2unix command to convert text files from the DOS/Windows format to the Unix/Linux format. The dos2unix command is a simple and effective tool that removes the carriage return characters and ensures compatibility when transferring files between Windows and Unix-like systems. You will also explore how to automate the conversion process using shell scripts. The dos2unix command is part of the tofrodos package, which is already installed in your Ubuntu 22.04 environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/cat -.-> lab-422648{{"`Linux dos2unix Command with Practical Examples`"}} linux/rm -.-> lab-422648{{"`Linux dos2unix Command with Practical Examples`"}} linux/chmod -.-> lab-422648{{"`Linux dos2unix Command with Practical Examples`"}} end

Introduction to the dos2unix Command

In this step, you will learn about the dos2unix command, which is used to convert text files from the DOS/Windows format to the Unix/Linux format.

The DOS/Windows format uses the carriage return and line feed (CR+LF) characters to indicate the end of a line, while the Unix/Linux format uses only the line feed (LF) character. This difference can cause issues when transferring files between Windows and Unix-like systems.

The dos2unix command is a simple and effective tool to address this problem. It reads the input file, removes the carriage return characters, and writes the modified content to the output file.

Let's start by checking the version of the dos2unix command installed on your system:

dos2unix --version

Example output:

dos2unix (NLS version)
Copyright (C) 2009-2022 Bernd Johannes Wuebben.
Copyright (C) 1994-1995 Benjamin Lin.
dos2unix comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of dos2unix under the terms of the GNU
General Public License. For more information about these matters,
see the file named COPYING.

The dos2unix command is part of the tofrodos package, which is already installed in your Ubuntu 22.04 environment.

Converting Text Files from DOS to Unix Format

In this step, you will learn how to use the dos2unix command to convert text files from the DOS/Windows format to the Unix/Linux format.

First, let's create a sample text file in the DOS format:

echo "This is a sample text file in DOS format." > sample_dos.txt

Now, let's use the dos2unix command to convert the file:

dos2unix sample_dos.txt

Example output:

dos2unix: converting file sample_dos.txt to Unix format ...

The dos2unix command has modified the file in-place, removing the carriage return characters and converting the file to the Unix format.

Let's verify the contents of the file:

cat sample_dos.txt

Example output:

This is a sample text file in DOS format.

As you can see, the file now uses the Unix line ending (LF) instead of the DOS line ending (CR+LF).

You can also specify an output file to save the converted file with a different name:

dos2unix sample_dos.txt sample_unix.txt

This will create a new file sample_unix.txt with the Unix format, while leaving the original sample_dos.txt file unchanged.

Automating dos2unix Conversion with Shell Scripts

In this step, you will learn how to automate the process of converting text files from DOS to Unix format using shell scripts.

Let's create a simple shell script to convert all .txt files in the current directory:

#!/bin/bash

for file in *.txt; do
    if [ -f "$file" ]; then
        echo "Converting $file to Unix format..."
        dos2unix "$file"
    fi
done

echo "DOS to Unix conversion complete."

Save this script as convert_to_unix.sh in the ~/project directory.

Now, make the script executable:

chmod +x ~/project/convert_to_unix.sh

You can now run the script to convert all .txt files in the current directory:

~/project/convert_to_unix.sh

Example output:

Converting sample_dos.txt to Unix format...
dos2unix: converting file sample_dos.txt to Unix format ...
DOS to Unix conversion complete.

The script uses a for loop to iterate through all the .txt files in the current directory, and then calls the dos2unix command on each file to convert it to the Unix format.

You can customize the script to handle different file extensions or even to recursively convert files in subdirectories. Additionally, you can add error handling and logging to make the script more robust.

Summary

In this lab, you learned about the dos2unix command, which is used to convert text files from the DOS/Windows format to the Unix/Linux format. You created a sample text file in the DOS format and used the dos2unix command to convert it to the Unix format. You also learned how to automate the dos2unix conversion process using shell scripts.

The dos2unix command is a simple and effective tool to address the issue of line ending differences between Windows and Unix-like systems. It removes the carriage return characters and converts the file to the Unix format. You explored the usage of the dos2unix command and verified the contents of the converted file.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like