神刀安全网

Enable a Confirmation When Deleting Files & Folders with rm Command

Enable a Confirmation When Deleting Files & Folders with rm Command 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 When Deleting Files & Folders with rm Command

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 .

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Enable a Confirmation When Deleting Files & Folders with rm Command

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址