Blog

Thoughts from my daily grind

Linux / BSD / macOS Terminal - Confirm before deleting files

Posted by Ziyan Junaideen |Published: 29 September 2021 |Category: IT
Delete / Erase |

A few months back, the NMRA (the National Medicines Regulatory Authority) lost 2000GB of data from the Sri Lanka Government Cloud when a "maintenance engineer" "accidentally" deleted the files. People pointed out how computers prompt confirmation before deletion and others pointed out how it is not so in Linux.

Indeed, Linux out of the box doesn't prompt confirmation before deleting a file, but we could make it to do so. I have done this numerous times when handing over a Linux server to a client. I include a separate user account with particular features that will be safe to use:

A user logged in using that account will:

  • Have sufficient permission to run commands and perform maintenance (ex: clearing log files).
  • Have file permissions that would prevent the deletion of essential files required by the program.
  • Confirm before deleting a file.

Let's look at the man page for rm, the file/folder deletion command.

RM(1)                     BSD General Commands Manual                    RM(1)

NAME
     rm, unlink -- remove directory entries

SYNOPSIS
     rm [-dfiPRrvW] file ...
     unlink file

DESCRIPTION
     The rm utility attempts to remove the non-directory type files specified
     on the command line.  If the permissions of the file do not permit writ-
     ing, and the standard input device is a terminal, the user is prompted
     (on the standard error output) for confirmation.

     The options are as follows:

     -d          Attempt to remove directories as well as other types of
                 files.

     -f          Attempt to remove the files without prompting for confirma-
                 tion, regardless of the file's permissions.  If the file does
                 not exist, do not display a diagnostic message or modify the
                 exit status to reflect an error.  The -f option overrides any
                 previous -i options.

     -i          Request confirmation before attempting to remove each file,
                 regardless of the file's permissions, or whether or not the
                 standard input device is a terminal.  The -i option overrides
                 any previous -f options.

# .
# ..
# ...

We can see that the -i option will prompt confirmation before deleting a file.

Example:

rm -i /path/to/file
remove /path/to/file?

If or not the user enters the -i option, we need to prompt for confirmation. We can achieve this by using an alias. Then whenever the user uses rm /path/to/file, the command that gets executed is actually rm -i /path/to/file.

alias rm="rm -i"

You should place this command can be placed on:

  • .bashrc - if you are using bash
  • .zshrc - if you are using ZSH
ziyan@iMac:~/Desktop|⇒  rm හරීන්\ හරිද\?
remove හරීන් හරිද??

So does this mean minister Harin Fernando with no Linux experience was correct and Sri Lankan Linux experts got it wrong, again?

Tags
About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments