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. Shapefile点图层转换为Shapefile线图层

    在Oracle数据表转换为Shapefile(一)和Oracle数据表转换为Shapefile(二)两篇文章中,分别介绍了两种不同的根据Oracle数据表生成Shapefile点图层的方法.本文在此基 ...

  2. Partition--分区Demo

    --============================================================= --创建分区函数 --创建500分区,分区键按照1000依次递增 CRE ...

  3. ecahrt 扇形(半扇形)

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

  4. C# 等待框

    今天发现dev控件自带了等待框,调用方便,而且不会阻塞主线程. 拉一个窗体,界面上放两个按钮,显示等待框,隐藏. <Window x:Class="WaitDialogTest.Mai ...

  5. C# 调用C++动态库注意事项

    C# 调用C++动态库注意事项 最近项目上需要在C#中调用C++,期间遇到不少坑,总结如下: 1.in const char*   对应C#中string 或  IntPtr 2.out const ...

  6. ODBC方式操作oracle数据库

    ODBC方式操作oracle数据库 一.查询语句: var strConn = System.Configuration.ConfigurationManager.ConnectionStrings[ ...

  7. 小程序:web-view采坑指南

    最近负责开发的[广州医保查询]小程序已经发布上线,其中使用web-view组件完成的[在线绑定社保卡]核心流程,遇到了一些坑,现总结如下: 首先,让我们一起看看什么是web-view ? 小程序api ...

  8. ERWin7.2在Windows 8.1中生成数据库遇到的问题!

    我在Windows 8.1中使用ERWin7.2,想象以前在windows 7 或者XP里那样先预览一下要生成的SQL语句,结果我发现居然没有“preview”按钮了,自然也就无法看到要生成的SQL语 ...

  9. UINavigationController及顶部导航条

    UINavigationController管理一个VC的栈,栈底的VC叫做这个UINavigationController的root view controller. 有一个函数叫做popToRoo ...

  10. php sapi 产生core 文件

    php sapi 产生core 文件 1) vim /usr/local/php7.1.6-debug/etc/php-fpm.conf rlimit_core = 0 改为 rlimit_core ...