Enable a Confirmation When Deleting Files & Folders with rm Command

Apr 21, 2016 - 5 Comments

Terminal in OS X Most command line users know that the “rm” command for removing and deleting files is quite powerful, able to delete just about any file imaginable within the file system – whether it should be removed or not. Once you add in wildcards and sudo, rm and srm because exponentially more potent and potentially dangerous, so for advanced users who wish to add in a layer of security to the rm feature they can enable a confirmation dialog with the rm and srm commands. This also offers a helpful protective mechanism for those who are learning the command line and want to place a a layer of verification between the instantaneous deletion of files and their command execution.

There are two parts to this trick, the first is simply knowing the proper flag to enable and use a confirmation before a file or folder is deleted with rm, and the second uses an alias to make the aforementioned remove with confirmation into the new default option for the rm command. Both of these tricks work in Mac OS X, linux, and most other unix variations, so it’s largely operating system agnostic, and it also works with the powerful srm secure remove command too. This is obviously aimed at more advanced users are comfortable with the command line to begin with, as using rm and srm are not appropriate tools for novices.

Using rm Command with Confirmation Before Deleting Files and Folders

The syntax to enable a confirmation before removing any files with rm (or srm) is simply an -i flag, used like so:

rm -i filename

For example, if you were to delete a file named “theSampleFile.zip” and wanted to have a confirmation before the removal of the command, you’d use the following syntax:

rm -i theSampleFile.zip

Once you hit return, you will be asked whether or not you wish to remove the file name in question, with the command repeating back the exact file before it deletes it like so:

% rm -i theSampleFile.zip
remove theSampleFile.zip? y

Hitting ‘y’ and return responds with a ‘yes’ to delete the file, and hitting ‘n’ and return key responds with a no and the file will not be removed.

The rm -i syntax even works with -r for getting a confirmation before recursively deleting directories and file contents of contained subfolders:

rm -ir /Example/Folder/

Again you’ll have to issue a y or n before the command will complete for each individual file found within the directory.

Getting the same confirmation dialog with srm also uses the -i flag:

srm -i /Example/file.zip

Again, you’d use the y and n keys to confirm or deny the removal of specified files.

Enable a confirmation before removing a file with rm and srm

How to Enable an ‘rm’ Command Confirmation by Default with an Alias

Open your .bash_profile or .profile and you can create an alias like so to change the default ‘rm’ syntax to ‘rm -i’ to be the new default.

alias rm='rm -i'

You can create the same type of alias with srm like so:

alias srm='srm -i'

Adding both of those to unique lines within the profile is sufficient, then refreshing the shell will allow both to be accessed from bash, zsh, tcsh, or whichever your shell is in use.

Have any other advanced tips for safeguarding rm and srm at the command line before removing files? Let us know in the comments. And if you just want to learn more interesting command line tips, browse our posts on the broad subject.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

5 Comments

» Comments RSS Feed

  1. auntchilada says:

    it is considered bad form to create an alias with the same name as a command.

  2. Fnordmeister says:

    Bad idea, because when one day when you work on another system, you’ll type rm, thinking the -i flag is automatic, and it won’t be.

    A better solution is to define

    alias del=”rm -i”

    That way you won’t get used to typing rm and expecting the -i flag.

  3. Bobber says:

    If you’re deleting a lot of files with wildcards, it can be a real pain to answer yes to every one of them. To see what’s going to be deleted before doing so, use the same wildcard with the ls command:

    ls *xyz*.txt

    Review the listing. If you want to delete them all, enter:

    rm *xyz*.txt

  4. Will P. says:

    Along the same lines, if you want to prevent overwriting files on the command line, open your .bash_profile or .profile and add:

    set -o noclobber

    Refresh the shell, and your files have an additional level of protection on the command line.

  5. rpk says:

    Copy and move also have the same flag:

    cp -i
    mv -i

    If you want to override an alias with the -i because you don’t want to
    confirm deletion on a bunch of files, just precede the command with
    a forward slash:

    \rm filenames

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site