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. .NET WCF Return String 字符串有反斜杠的处理

    应该是: {"Message":"Hello World"} 结果是:" {\"Message\":\"Hello Wo ...

  2. android ART-逆向研究者的福音?

    android 4.4起,提供了一种与Dalvik截然不同的运行环境-ART(Android Runtime)的支持.目前用户可以选择设备的运行环境,在不久的将来ART肯定会替代Dalvik Runt ...

  3. ecahrt 扇形(半扇形)

    var data = [{ "name": "1", "value": 54 }, { "name": "2& ...

  4. [TJOI2007] 线段

    因为每行必须走完才能到下一行,所以我们有两种决策: 1.最后留在线段左端点 2.最后留在线段右端点 这种存在状态转移且多决策的问题用动态规划来进行递推是最好不过的了. 所以我们设\(dp[i][0/1 ...

  5. 该用li还是得用

    如:这样子少一层是很好,但为了美观,后面都补上a,但鼠标经过也会有背景色,所以还是得多一层li 解决后:

  6. 洛谷P4337 [ZJOI2018]线图(状压+搜索+乱搞)

    题面 传送门 题解 妈呀调了我整整一天-- 题解太长了不写了可以去看\(shadowice\)巨巨的 //minamoto #include<bits/stdc++.h> #define ...

  7. HTML5 调用百度地图API地理定位

    <!DOCTYPE html> <html> <title>HTML5 HTML5 调用百度地图API地理定位实例</title> <head&g ...

  8. 返回参数去掉xml格式,以纯json格式返回(转)

    Json 格式显示public static void Register(HttpConfiguration config) { //////////////设置不以xml格式返回 config.Fo ...

  9. 04day->python列表和元祖

    一.列表 1.索引.切片     索引:根据索引值获取,里表里的值     切片:和字符串相似 2.增     1)append(object),在列表的末端添加     2)insert(index ...

  10. Object类和包装类的一些方法

    一.instanceof关键字的使用: a instanceof A:判断对象a是否是类A的实例.如果是,返回true:如果不是,返回false. 使用场景:为了避免在向下转型时出现ClassCast ...