-3

So basically I am trying to create a script in bash shell which will emulate the windows delete features.

So instead of deleting a file or a folder it will move it to the recycle bin. I've got everything working apart from an issue I am having with moving folders into the recycle bin.

For example if I have a empty folder and run my script safe_rm it will not move the folder to the bin. However if I have a child folder in that parent folder it will delete both parent and child, so

safe_rm -r dir1/ <--wont delete

but

safe_rm dir1/ with content of dir2 file1 (dir1/file1 dir1/dir2) will delete.

Here is my code i am having an issue with

moveFilesFolder(){
 #check if file/directory exists
    if [ $(checkFileFolder $1) == "true" ]
    then
      movingFilesToRemove=$(ls $1)
      #if there's too many files/directories then send them to the moveFiles functions
      if [ ${#movingFilesToRemove[*]} -gt 1 ]
      then
        movingFiles $movingFilesToRemove
      else
      #folder handling  
        if [ $(isFolder $1) == "true" ]
        then
          #we check the rR flag for selected folder  
          if [ $optionRFlag == "false" ]
          then
            echo "rm: cannot remove \`$1': Is a directory"
         else
           lvlDown=true
           #we check if the I argument used on the folder,
               #which prompts the user
           if [ $optionIFlag == "true" ] && [ $(isFolderEmpty $1) == "false" ]
            then
              read -p "rm: descend into directory \`$1'?" downFolder
              case $downFolder in
              y* | Y*)
                lvlDown="true" ;;
              *)
                lvlDown="false" ;;
              esac

            fi
            if [ $lvlDown == "true" ]
            then
              #now we display all the descending folders and gives full path
              #i will be using the sed command to handle sub files 
              #this will proceed every item with present working folde
          subfiles=$(ls $1 | sed "s;^;`pwd`/$1;")
           # subfiles=$(ls $1 | grep '^d' )
           # subfiles=$(ls -R | grep ":" | sed "s/://" )  

           movingFiles $subfiles
            #now we move the empty folder 
              if [ $(isFolderEmpty $1) == "false" ]  
              then
                dlt=true
    if [ $optionIFlag == "true" ] 
    then
                 read -p "rm: remove directory \`$1'?" deleteFolder
                 case $deleteFolder in
                 y* | Y*)
                    dlt="true" ;;
                 *)
                    dlt="false" ;;
                 esac
               fi

               if [ $dlt == "true" ]
               then
                 mv $1 $recycleFolder
                echo `pwd` 
                 if [ $optionVFlag == "true" ]
                 then
                   echo "removed directory: \`$1'"
                 fi
               fi
             fi
           fi
         fi
       else
         #here we are dealing with file handling 
         agreed=true
         if [ $optionIFlag == "true" ]
         then
           read -p "$(interMessage $1)" userAccepts
           case $userAccepts in
           y* | Y*)
             agreed="true" ;;
           *)
             agreed="false" ;;
           esac
         fi
         #refer to above i flag
         if [ $agreed == "true" ]
         then

           mv $1 $recycleFolder
           echo `pwd`
           if [ $optionVFlag == "true" ]
           then
             echo "removed \`$1'"
           fi
         fi
       fi
     fi
   else

     echo "rm: cannot remove \`$1': No such file or directory"
   fi

}
4

2 回答 2

4

请参阅 comp.unix.questions 常见问题解答,问题 3.6:“我如何“取消删除”文件?”

http://www.faqs.org/faqs/unix-faq/faq/part3/

两点:首先,这通常被认为是一个主意。你会变得依赖于“rm”的这种行为,有一天你会发现自己在一个“rm”实际上是“rm”的正常系统上,你会给自己带来麻烦。其次,您最终会发现处理磁盘空间和维护垃圾箱所涉及的时间的麻烦,只是对“rm”更加小心一点可能会更容易。对于初学者,您应该在手册中查找“rm”的“-i”选项。

答案中提到的 MIT 删除/取消删除实用程序套件可以在以下位置找到

http://ftp.funet.fi/pub/archive/comp.sources.misc/volume17/delete/

于 2012-07-13T19:13:55.937 回答
1

垃圾生活的地方

大多数 Linux 系统上的垃圾都存在于~/.local/share/Trash/{expunged,files,info}. 跟踪元信息涉及一些子目录,但~/.local/share/Trash/files如果您不担心完全符合垃圾规范,您可以将文件或目录扔进其中。

穷人的垃圾

即使您不关心完全遵守垃圾规范,您仍然可以使用垃圾设施,同时确保您遵守其关键要求之一。垃圾规范说:

即使具有相同名称和位置的文件被多次删除,后续的每次删除都不得覆盖以前的副本。

您可以使用基本的 shell 功能来满足要求。例如:

trash () {
    local trashdir="$HOME/.local/share/Trash/files"
    mkdir -p "$trashdir"
    mv --backup=numbered "$@" "${trashdir}/"
}

使用 CLI 工具

如果您使用的是基于 Debian 的系统,则可以安装垃圾-cli。安装后,您可以直接调用它或将其命名为透明使用。例如:

sudo aptitude install trash-cli
alias rm='trash'

mkdir /tmp/foobarbaz
touch /tmp/foobarbaz
rm -rf /tmp/foobarbaz

请注意,这似乎不能很好地处理 Ubuntu 的 ecryptfs 安装的主目录,但否则可以让您开箱即用地遵守垃圾规范,而无需重新发明轮子。

于 2012-07-14T02:03:05.170 回答