dos2unix: Handling Permission Errors

Overview

When converting a file from Windows (DOS) line endings to Unix line endings, the usual `dos2unix` syntax may sometimes result in a permission error.

For example:

dos2unix filename

may produce an error similar to:

dos2unix: Failed to change the permissions of temporary output file: Operation not permitted
This can happen when `dos2unix` does not have the necessary permissions to modify or replace the original file.

Solution

Instead of converting the file in-place using the standard syntax, use the `-n` option to explicitly specify an input (source) file and an output (target) file:

dos2unix -n <source_file> <target_file>

For example:

dos2unix -n input.txt output.txt

This converts `input.txt` to Unix line endings and saves the result as `output.txt`, leaving the original file unchanged.

Overwriting the Original File

If you want to convert the file while keeping the same filename, you can specify the same path as both the source and target:

dos2unix -n /path/to/file.txt /path/to/file.txt

This rewrites the file in Unix format without relying on `dos2unix` to replace the original file in the usual way.

Summary

Command Purpose
———————————– —————————————
`dos2unix file.txt` Convert a file in-place (may throw an error
`dos2unix -n source.txt target.txt` Convert to a separate new file
`dos2unix -n file.txt file.txt` Convert while keeping the same filename