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. 一些参考网站 - Reference Documentation - Website Address

    Reference Documentation - Website Address MSDN Visual Studio 2015官方文档 https://msdn.microsoft.com/zh- ...

  2. AJAX方式调用百度天气

    后台代码: [HttpPost] public string AjaxWeather() { string CityName = string.IsNullOrEmpty(Request.Form[& ...

  3. Javascript获取select的选中值和选中文本(转载)

    var obj = document.getElementById(”select_id”); //selectid var index = obj.selectedIndex; // 选中索引 va ...

  4. Tomcat源码(一):整体架构

    由于tomcat的组件较多,处理流程比较复杂 ,这里是 由浅到深来解释tomcat的整体架构 1.首先应该大致了解下tomcat的 /conf/server.xml  配置文件:在tomcat启动的时 ...

  5. eclipse 中文或法文等语言注释错误解决办法 Some characters cannot be mapped using "GBK" character encoding

    这个问题会造成 无法修改包名.解决办法: Window->Preferences->Content Types->Text->Java Source File  Default ...

  6. 南昌网络赛 I. Max answer 单调栈

    Max answer 题目链接 https://nanti.jisuanke.com/t/38228 Describe Alice has a magic array. She suggests th ...

  7. 3.iptables 扩展模块

    --tcp-flags 用于匹配报文的tcp头的标志位 iptables -t filter -I INPUT -p tcp -m tcp --dport 22  --tcp-flags SYN,AC ...

  8. Tomcat内存溢出解决java.lang.OutOfMemoryError: PermGen space

    背景:把两个项目同时部署在tomcat,启动快好的时候,报java.lang.OutOfMemoryError: PermGen space 原因:因为两个项目的jar包太多,JVM把里面的class ...

  9. [独家] Adobe Flash 直接复制元件不改变原元件

    正在考网页高级考证,然后会需要做Adobe Flash相关操作.在网上看了下,基本都是错误的.于是,自己研究解决该问题. 首先,你需要更改元件不改变原文件是不可能的. 所以: 1.你需要对原元件所在的 ...

  10. iOS开发之像素Compositing

    假如Layer S·在Layer D上面,则最终的屏幕的颜色值如下: \[R = S + D \cdot (1- S_\alpha)\] \(R\): 最终的RGB \(S\): source col ...