日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rm命令改为移动到回收站

發(fā)布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rm命令改为移动到回收站 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

環(huán)境:

Ubuntu16.04 64位有效

ubuntu18.10下面無效(18.10的在文末補(bǔ)充)

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權(quán)限下:

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工具,其實(shí)就是回收站的命令行模式:? ? ? ? ? ? ?

$ sudo apt-get install trash-cli

?


第二步:
#給trash命令添加別名”rm”,覆蓋系統(tǒng)rm命令? ? ? ? ? ??

$ vi ~/.bashrc

#在最后一行加入:

alias rm="trash"

?#保存并退出? ? ? ? ? ??

$ source ~/.profile

?

以后不必刪文件夾時加參數(shù) -r 直接使用 rm file or ?rm filedirectory

刪除文件或文件夾,且刪除后,它就會自動跑去回收站了!

?

總結(jié)

以上是生活随笔為你收集整理的rm命令改为移动到回收站的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。