reference:http://www.coolcoder.in/2014/01/getting-over-dangers-of-rm-command-in.html

When we want to delete a directory and everything in it, we normally usually use rm -rf. However, this  is a bad habit to get into. If you make a mistake, you could delete your entire partition.
 
For a start, if you drop the '-f' then at least it won't go blindly ahead and delete everything regardless of permissions etc. You can also use the ‘-i’ option which prompts you for confirmation. Deleting stuff from KDE/Gnome usually moves the file to .local/share/Trash, so if you delete from a GUI rather than CLI, then you've got that safety net.
 
People have talked about how you could set up an alias or write a little script which replaces the rm command with a safer mv command which shifts the files to .local/share/Trash where after you could delete them from the trash icon on your desktop. Others warn of caution when using this approach as when you go to a new machine and start removing stuff left right and center without thinking, then you can do bad things very quickly.
 
 A compromise might be to setup the alias/script for safe deletion, that way you won't get into a problem.
One of the alias could be rm='rm -i', which will make the system prompt you before deletion. But, this too at times become horror because after using it for a while , you will expect rm to prompt you by default before removing files. Of course, one day you'll run it with an account that hasn't that alias set and before you understand what's going on, it is too late. 
 
One other way to disable rm command can be:
 
alias rm='echo  "rm is disabled, use trash or /bin/rm instead."'
 
Then you can create your own safe alias, e.g.
 
alias remove='rm -irv'
 
The other way is using some script alternative, one of them can be 
 
1.Take a variable, for example: TRASHPATH
2.Assign the exact path of trash to that variable; most likely, trash path will be .local/share/Trash
3.Now, here's the code just paste this into your .bashrc file.
   
##################code starts
TRASHPATH=.local/share/Trash
function rem()
{
for i in $*
do
mv $i $TRASHPATH/$i
done
}
##################code ends
 
Now,if you want to delete files just issue the command
rem file1
 
You can delete multiple files as well,
rem file1 file2 file3 file4 file5
 
 
Instead of trash, you can redirect to another folder also, just don't include a slash(/) at the end of path in TRASHPATH variable.
 
Another alternative can be using trash command instead. It is compiled Objective-C and cleverly uses standard file system APIs and, should it fail (i.e. user doesn't have sufficient rights), it calls Finder to trash the files (effectively prompting for authentication). You can read more info of trash from hasseg.org/blog.

- See more at: http://www.coolcoder.in/2014/01/getting-over-dangers-of-rm-command-in.html#sthash.AnASjER1.dpuf

Getting over the dangers of rm command in Linux---reference的更多相关文章

  1. Sending Email from mailx Command in Linux Using Gmail’s SMTP

    The mailx or mail command in Linux is still providing service for guys like me, especially when we n ...

  2. Using Android Phone to recover SD card formatted with DD command under linux

    Using Android Phone to recover SD card formatted with DD command under linux 1. Formatted a sd card ...

  3. screen command of linux

    [screen command of linux] 常用键:   补充: Ctrl-a S  # split terminal horizon Ctrl-a TAB   # switch to ano ...

  4. 下载tree命令的源代码 - The Tree Command for Linux Homepage

    The Tree Command for Linux Homepage http://mama.indstate.edu/users/ice/tree/ [root@test ~]# ll -as m ...

  5. The dd command of linux

    The dd command stands for "data duplicator" and used for copying and converting data. It i ...

  6. Chrome-Console( Command Line API Reference)

    来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference The Comma ...

  7. Changing the Color of Linux ls Command 改变Linux的ls命令显示的颜色

    Linux command ls basically use the file /etc/DIR_COLORS or /etc/DIR_COLORS.xterm to define the color ...

  8. Switch User Command in Linux

    Switch user command (su) has the following forms: su Switch to , without loading environment variabl ...

  9. logrotate command in Linux

    背景 在生产过程中,由于磁盘空间.保留周期等因素,会对系统.应用等日志提出要求,要求系统日志定期进行轮转.压缩和删除,从而减少开销,而系统自带的logrotate  则是一个简单又实用的小工具,下面着 ...

随机推荐

  1. Echarts X轴内容过长自动隐藏,鼠标移动上去显示全部名称方法

    最近公司做项目,使用echarts做开发,碰到一些数据的名称很长导致图例展示的效果不是很好,自己写了一个方法,当X轴内容过长时自动隐藏,鼠标移动上去显示全部名称 样例: 图二是鼠标移动到名称显示的,怎 ...

  2. [LeetCode 题解]: Maximum Subarray

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Find the c ...

  3. [SIP01]SIP Header Fields里面各字段用途

    INVITE sip:bob@biloxi.com SIP/2.0 Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds Max-Forw ...

  4. Nginx源码编译

    1. 概述 有时由于添加了自己编写的c++模块,或者改了源码的一些名称.配置什么的,需要自行编译nginx. 可以先下下来源码,然后需要的话就自己改下源码或增加模块,最后再编译成linux或者wind ...

  5. 使用System.Data.SQLite及其EF模块操作SQLite数据库(文件)

    SQLite for .NET (System.Data.SQLite) introduction An ADO.NET provider for SQLite including EF and Li ...

  6. Sql2012 AlwaysOn

    准备: 两台宿主机,两台虚拟机,一根交叉线 系统都是windows server 2012 步骤 1.两台虚拟机均为双网卡 2.配置故障转移群集 3.执行AlwaysOn配置(增加Listen此步骤上 ...

  7. [转载] Linux 下产生和调试core文件

    原地址:http://blog.csdn.net/shaovey/article/details/2744487 linux下如何产生core,调试core 在程序不寻常退出时,内核会在当前工作目录下 ...

  8. [Perl]通过GD模块获取字符/汉字的点阵

    原来Perl获取汉字的点阵如此简单 示例脚本应保存为utf8格式 Code: [全选] [展开/收缩] [Download] (Untitled.pl) use GD; use utf8; my $g ...

  9. 【OCP|052】OCP换题库,052最新题库及答案整理-第10题

    10.Which two are true about consistent database backups? A) They can only be taken when a RECOVERY C ...

  10. ajax 提交 json格式数据到后台

    例子:$.ajax({ type: 'POST', url: "/ROOT/modify.do", contentType: "application/json" ...