博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Getting over the dangers of rm command in Linux---reference
阅读量:6582 次
发布时间:2019-06-24

本文共 2826 字,大约阅读时间需要 9 分钟。

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 .
- See more at: http://www.coolcoder.in/2014/01/getting-over-dangers-of-rm-command-in.html#sthash.AnASjER1.dpuf

转载地址:http://xzino.baihongyu.com/

你可能感兴趣的文章
eclipse缺省的Server没有weblogic
查看>>
The Java serialization algorithm revealed---reference
查看>>
使用WMI来连接远端计算机
查看>>
Linux下的线程
查看>>
Fat-jar 打包,并使用 proguard 混淆代码
查看>>
[转]jquery.pagination.js分页
查看>>
C#泛类型链表的实现
查看>>
升级到WP8必需知道的13个特性
查看>>
Ext.getCmp()的简单使用
查看>>
前端样板资源概览及总评
查看>>
atitit.常用编程语言的性能比较 c c++ java
查看>>
[开发笔记]-实现winform半透明毛玻璃效果
查看>>
一款非常棒的纯CSS3 3D菜单演示及制作教程
查看>>
CSS3+HTML5特效8 - 顶部和右侧固定,左侧随窗口变化的布局
查看>>
利用c#+jquery+echarts生成统计报表(附源代码)
查看>>
簡單SQL存儲過程實例
查看>>
于西蔓_百度百科
查看>>
AMD and CMD are dead之KMD.js依赖可视化工具发布
查看>>
网页刷新方法集合
查看>>
[Android]对BaseAdapter中ViewHolder编写简化(转)
查看>>