Getting over the dangers of rm command in Linux---reference
2014-01-21 19:07
441 查看
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
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
- the diary for one command application of pipe in Linux
- the diary of sleep jobs & fg command line in linux
- Get and display the size of file and directory in Linux system using du command 获取和现实linux文件大小(三)
- php 报错Deprecated: Assigning the return value of new by reference is deprecated in的解决办法
- The number of method references in a .dex file cannot exceed 64K. Error 解决方案
- 方法数超过64K:The number of method references in a .dex file cannot exceed 64K.
- Android开发 Error(建议收藏下来以备不时之需):The number of method references in a .dex file cannot exceed 64K.
- Solve: Your project references the latest version of Entity Framework (for MySQL) in Visual Studio 2013
- How to Defeat Windows 8 ASLR in Getting the Address of KPCR
- An A-Z Index of the Bash command line for Linux.摘自https://ss64.com
- Valgrind: a toolkit of command check over Linux/Unix
- Reference in the manifest does not match the identity of the downloaded assembly
- 7 Strace Examples to Debug the Execution of a Program in Linux
- Getting a statistics education: Review of the MSc in Statistics (Sheffield)
- Deprecated: Assigning the return value of new by reference is deprecated in……解决方法
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- Error:The number of method references in a .dex file cannot exceed 64K. 解决方案
- Increasing the maximum number of tcp/ip connections in linux
- xxxx.accessor: The reference to 'xxxx' was not found in the list of this projects references