linux - 模拟 Windows,如 UNIX 上的删除

标签 linux bash unix

所以基本上我试图在 bash shell 中创建一个脚本来模拟 Windows 删除功能。

因此,它不会删除文件或文件夹,而是将其移动到回收站。除了将文件夹移入回收站时遇到的问题之外,一切都正常。

例如,如果我有一个空文件夹并运行脚本 safe_rm,它不会将该文件夹移动到垃圾箱。但是,如果我在该父文件夹中有一个子文件夹,它将删除父文件夹和子文件夹,所以

safe_rm -r dir1/<--不会删除

但是

safe_rm dir1/包含 dir2 file1 (dir1/file1 dir1/dir2) 内容的内容将被删除。

这是我遇到问题的代码

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

}

最佳答案

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

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

Two points: first, this is generally accepted as a bad idea. You will become dependent upon this behaviour of "rm", and you will find yourself someday on a normal system where "rm" is really "rm", and you will get yourself in trouble. Second, you will eventually find that the hassle of dealing with the disk space and time involved in maintaining the trash bin, it might be easier just to be a bit more careful with "rm". For starters, you should look up the "-i" option to "rm" in your manual.

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

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

关于linux - 模拟 Windows,如 UNIX 上的删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11476728/

相关文章:

linux - 如果 $? 扫描整个代码= 0 或 1

linux - 如何优化这两行bash代码

linux - 使用条件 linux 管道输出流

bash - 在 bash 'for loop' 结束时打印结果

c - 关于 UNIX 作为虚拟机和 C 作为脚本语言的引用来源

c++ - 在命令行中将输入显示到数组中时,如何删除./a.out?

linux - 无法通过 CLI 调试 Jmeter

bash - bash 中前导零的范围

linux - 将 CD 文件上的 sha256sum 输出到文本文件

linux - 如何将函数/别名定义写入 Bash 历史记录?