rm命令改为移动到回收站
環境:
Ubuntu16.04 64位有效
ubuntu18.10下面無效(18.10的在文末補充)
xfce4
rm.sh來自:
https://github.com/a14m/rm/blob/master/rm.sh
具體代碼如下:
#!/bin/bash# VARIABLES VERSION="0.3" TO_DELETE=() RECURSIVE=false FOREVER=false VERBOSE=false# FUNCTIONS function show_help() {echo -e ""echo -e "This is custom script used to avoid deleting files forever"echo -e "instead files will be moved to $HOME/.Trash folder"echo -e "and this script only works if you invoked it directly from the terminal"echo -e "it doesn't work if for example git used rm or if you used rm in other scripts"echo -e ""echo -e "usage: rm [OPTION]...FILE..."echo -e "Move FILE(s) to $HOME/.Trash folder"echo -e ""echo -e "Options:"echo -e " -h, --help \t\tdisplay this help text and exit."echo -e " --version \t\toutput version information and exit."echo -e " -r, -R, --recursive,\tmove the directories and their content to the Trash folder."echo -e " -f, --forever,\tremove the directories and their content to the Trash folder."echo -e " \tuse this to delete files or empty trash it calls the /bin/rm"echo -e ""echo -e ""echo -e "Script source 'https://github.com/artmees/rm'"echo -e "github page 'https://artmees.github.io/rm'"echo -e ""echo -e "Install :"echo -e " sudo cp rm.sh /usr/local/bin/rm"echo -e "Uninstall :"echo -e " sudo rm /usr/local/bin/rm"echo -e ""echo -e "you need to restart the termial for this to work"echo -e "or you can use source ~/.bashrc or source ~/.bash_profile"echo -e ""echo -e "Use Old rm :"echo -e " /bin/rm"exit 0 }function check_flags() {# check the flags user usedif [ $# -le 0 ];thenshow_helpfiwhile test $# -gt 0; docase "$1" in-h|--help) show_help;;-r|-R|--recursive)shiftRECURSIVE=true;;-f|--forever)shiftFOREVER=true;;--version)echo -e "$VERSION"exit 0;;-v|--verbose)shiftVERBOSE=true;;-a|-b|-c|-d|-e|-g|-h|-i|-j|-k|-l|-m|-n|-o|-p|-q|-s|-t|-u|-w|-x|-y|-z|-A|-B|-C|-D|-E|-F|-G|-H|-I|-J|-K|-L|-M|-N|-O|-P|-Q|-S|-T|-U|-V|-W|-X|-Y|-Z)echo -e ""echo -e "Invalid Argument"show_help;;*)get_to_delete $1shift;;esacdone }# Helper method to populate the TO_DELETE array function get_to_delete() {while test $# -gt 0; doTO_DELETE=("${TO_DELETE[@]}" $1)shiftdone }function check_trash_directory() {# check that global system trash exists# if not create itif [ ! -d ~/.Trash/ ];thenmkdir ~/.Trashfi# check the files and flags passedcheck_flags $@if $FOREVER ;then# use the common system rm -r passing the same argumentsif $VERBOSE ;thenecho -e "`/bin/rm -r -v ${TO_DELETE[@]}`"else/bin/rm -r ${TO_DELETE[@]}fielsemove_to_trashfiexit 0 }# Move the deleted files to the trash # show output if -V option was specified function move_to_trash() {for i in "${TO_DELETE[@]}";doFILENAME=$iFILENAME+="_"FILENAME+=$(date +"%H_%M_%S")if [ -d $i ] && $RECURSIVE ;thenif $VERBOSE ;thenecho -e "`mv -v $i ~/.Trash/$FILENAME`"elsemv $i ~/.Trash/$FILENAMEfielif [ -d $i ] ;thenecho -e "$i is a directory please use --recursive to remove directories and their content"elif [ -f $i ] ;thenif $VERBOSE ;thenecho -e "`mv -v $i ~/.Trash/$FILENAME`"elsemv $i ~/.Trash/$FILENAMEfielse# TODO add other option similar to rm -f to not output this errorecho -e "No such file or directory '$i'"fidone }# Refer to http://stackoverflow.com/questions/20572934/get-the-name-of-the-caller-script-in-bash-script # to understand the next funtion # usage: determine if the script was invoked by a user or other script # if it was invoked by other scripts then use the system rm instead # to avoid messing with other scripts behaviour. function check_invoker() {PARENT_COMMAND=$(ps $PPID | tail -n 1 | awk "{print \$5}")if [ $PARENT_COMMAND == '-bash' ]; thencheck_trash_directory $@else/bin/rm $@exit 0fi }# Main check_invoker $@ exit 0?
root權限下:
cp rm.sh?/usr/bin
chmod u+x /usr/bin/rm.sh
chmod 777 /usr/bin/rm.sh
?
$HOME/.bashrc中加入下面一句
alias rm=/usr/bin/rm.sh
效果如下:
rm rm.sh
(python2.7) appleyuchi@ubuntu:~/.Trash$ ls
rm.sh_16_13_38
?
?
?
###################################################################################
下面方法在18.10和20.04測試通過
第一步:
#安裝trash-cli工具,其實就是回收站的命令行模式:? ? ? ? ? ? ?
?
第二步:
#給trash命令添加別名”rm”,覆蓋系統rm命令? ? ? ? ? ??
#在最后一行加入:
alias rm="trash"?#保存并退出? ? ? ? ? ??
$ source ~/.profile?
以后不必刪文件夾時加參數 -r 直接使用 rm file or ?rm filedirectory
刪除文件或文件夾,且刪除后,它就會自動跑去回收站了!
?
總結
以上是生活随笔為你收集整理的rm命令改为移动到回收站的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Adaboost 算法的原理与推导(转载
- 下一篇: 线性回归原理和实现基本认识(转载)