Skip to content

How to forbid output redirection to avoid overwrite the existing file

homepage-banner

Introduction

Output redirection is a useful feature in the command line that allows you to save the output of a command to a file. However, it can be dangerous if you’re not careful. If you accidentally redirect output to a file that already exists, you’ll overwrite the existing file and lose its contents. In this blog post, we’ll discuss how to forbid output redirection to avoid overwriting existing files.

How to forbid output redirection

In output redirection, > means redirect and overwrite existing file content. To prevent accidentally overwriting important content, you can use the following command:

set -C

This will prompt when redirecting output to an existing file:

cannot overwrite existing file

If you need to force overwrite, you can use the >| symbol:

>|

To disable this feature, use:

set +C
Leave a message