【问题标题】:Shell script to Delete Folders and Zip files in a folder which contains same name用于删除包含相同名称的文件夹中的文件夹和 Zip 文件的 Shell 脚本
【发布时间】:2016-06-12 02:39:52
【问题描述】:

我遇到了使用 shell 脚本的场景。如果文件夹和 Zip 名称相同,我需要删除它。任何人都可以帮助我吗?

示例

下面是脚本需要在目录中搜索同名的目录路径(这里需要打印删除example和example.zip

Path:/tmp/test/
/tmp/test/example
/tmp/test/example.zip
/tmp/test/zack

【问题讨论】:

    标签: bash shell sh


    【解决方案1】:

    这应该可以解决问题,甚至适用于 cronjob:

    find $DIR_PATH -type d -exec sh -c '[ -f "{}.zip" ] && rm -fr {}.zip {}' \;
    

    只需将 DIR_PATH 设置为您要搜索的位置

    【讨论】:

      【解决方案2】:

      您可以使用如下脚本:

      #!/bin/bash
      parent_dir=/tmp/test/
      for i in `ls /tmp`
      do
              if [[ $i != *.zip ]]; then
                      if [[ -f /tmp/$i.zip ]]; then
                              array=$array" "$i
                      fi
              fi
      done
      array=( $array )
      for i in ${array[@]}
      do
              rm -r $parent_dir$i
              rm -r $parent_dir$i".zip"
      done
      

      【讨论】:

      • 感谢您的脚本
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2012-07-24
      • 2019-05-04
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多