递归地将文件放在Unix中的回收站中

时间:2015-09-24 03:08:26

标签: bash shell unix recursion path

我编写了脚本来暂时删除Unix中的文件,然后保存文件路径以便可以恢复它们。我有2个函数,一个用于删除文件,另一个用于删除也递归删除文件的目录。我有适当的命令来识别目录名称和路径,以及检查文件是否保留在目录中然后删除它们。起初我得到一个无限循环,但我做了一些更改,现在,当我尝试删除目录时,它说没有这样的文件或目录。它仍然运行代码,但它不会删除任何文件。有人能想出来吗?

    #!/bin/bash
    #checks to see if deleted folder exists. If it doesn't, it is created.
    if [ ! -d ~/deleted ];
      then
          mkdir ~/deleted
    fi
    if [ ! -f ~/.restore.info ] ;
       then
          touch ~/.restore.info
    fi
    function recur_delete {
    dir=$1 #this will indicate the directory name only
    dirpath=$(dirname $dir) #gets the directory path
   if [ "$( ls -A /$dirpath/$dir)" ]; #determines if the directory contains files.
   then
      filename=$(find dirpath/dir -type f -printf "%f\n" | head -1)
      delete_file $filename #filename is found and sent to delete_file function to be deleted.
      recur_delete $dir #function is called again to see if more files are present.

     else
       echo Directory is empty, the directory will be deleted
       echo $dirpath/$dir >> ~/.restore.info
       rmdir ${dirpath/$dir}

fi

    }
    function delete_file {
    inode=$(stat -c%i $filename) #grabs inode # for the chosen filename.
    filename=$1 #reinitializes the variable filename as the first argument
    pwd=$(readlink -e $filename) #This gets the entire path for the chosen file
      if $interactive
        then
          if [ $verbose = true ];
                 then
                 read -p "Are you SURE you want to delete $filename ????" i_input
                     if [ $i_input == "y"  ] || [ $i_input == "Y" ];
                         then
                         mv $filename ~/deleted/${filename}_$inode
                         echo ${filename}_$inode:$pwd >> ~/.restore.info
                         echo $filename has been deleted. Congrats.
                             else
                             echo Nothing has been done, the file or files remain.
                     fi
                else
                    read -p "Are you SURE you want to delete $filename ????" i_input
                    if [ $i_input == "y"  ] || [ $i_input == "Y" ];
                      then
                      mv $filename ~/deleted/${filename}_$inode
                      echo ${filename}_$inode:$pwd >> ~/.restore.info
                         else
                         echo Aborted
                    fi
          fi
      elif $verbose
        then
         mv $filename ~/deleted/${filename}_$inode
         echo ${filename}_$inode:$pwd >> ~/.restore.info
         echo $filename has been deleted. Congrats.
      else
         mv $filename ~/deleted/${filename}_$inode
         echo ${filename}_$inode:$pwd >> ~/.restore.info
         echo Executed
    fi
    }
    interactive=false
    verbose=false
    recursive=false
    while getopts ivr OPTION
    do
      case $OPTION in
        i) interactive=true;;
        v) verbose=true;;
        r) recursive=true;;
      esac
    done
    shift $[OPTIND-1]
    for i in $*
      do
    filename=$i
    basefile=$(basename $i)
    if [ "$i" == "" ];
       then
         echo No file provided
        elif [ -d $filename ];
         then
            if [ $recursive = true ];
               then
                 recur_delete $filename
               else
               echo This is a directory, please provide a file name.
            fi
       elif [ ! -f $filename ];
        then
         echo File does not exist
       elif [ "$basefile" == "safe_rm" ];
         then
          echo Attempting to delete safe_rm - operation aborted!!!!
    #This is the line that takes the filename to be deleted and modifies the
    #experience based on what the user wants.
    else
       delete_file $filename
    fi
    done

1 个答案:

答案 0 :(得分:0)

这看起来非常复杂。如何建立一些东西

$ mkdir ~/.trashbin
$ mv /absolute/path/to/dir/or/file ~/.trashbin

用于临时删除,然后

$ mv ~/.trashbin/absolute/path/to/dir /absolute/path/to/dir

还原?

你需要一小段代码来删除旧的绝对路径,删除〜/ .trashbin,但这很简单。

<强>更新

啊,这是愚蠢的教授问题。我很像。

好的,这就是交易:在任何递归中,每次调用都必须在某种意义上“变小”,最后到达停止重复的程度。在您的情况下,这应该是ls -A的结果。离开代码并检查你真正从空目录中获取ls -A的内容,以及是否评估为0或非零。提示:我打赌它永远不会。