简体   繁体   English

编写一个 Bash 脚本,不使用 -find 删除目录和所有子目录中的所有零长度普通文件

[英]Writing a Bash script to remove all zero length ordinary files in the directory and all sub-directories without using -find

This is what I have but I am pretty sure this only removes the files in the directory and its direct subdirectories.这就是我所拥有的,但我很确定这只会删除目录及其直接子目录中的文件。 How would I target all subdirectories?我将如何定位所有子目录?

    #!/bin/bash
    currDir="$PWD" #the current working directory is set as the default

    if [ $# -eq 1 ] #if the number of command line args is 1
    then
         currDir=$1 #use the specified directory
    fi

    for file in $currDir/*; #iterate through all the files in the directory
    do
          if [ -s $file ] #if file exists and is not empty
          then
               continue #do not remove
          else
               rm -rf $file #if it exists and is empty, remove the file
          fi
          for file2 in $currDir/**/*; #remove empty files from subdirectory 
          do
              if [ -s $file2 ]
              then
                   continue
              else
                   rm -rf $file2
              fi
          done
     done

You can issue:您可以发出:

find /path/to/your/directory -size 0 -print -delete

This will delete all the files in a directory (and below) that are size zero.这将删除目录(及以下)中大小为零的所有文件。

Would you pleaae try the following:请您尝试以下操作:

#!/bin/bash

traverse() {
    local file
    for file in "$1"/*; do
        if [[ -d $file ]]; then
            traverse "$file"
        elif [[ -f $file && ! -s $file ]]; then
            echo "size zero: $file"
            # if the result is desirable, replace the line above with:
            # rm -f -- "$file"
        fi
    done
}

traverse .      # or set to your target directory

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写bash脚本在当前目录和子目录中顺序重命名文件 - Writing a bash script to rename files sequentially in current directory and sub-directories 如何gzip bash所有子目录下的所有文件 - How to gzip all files in all sub-directories in bash 删除除一个以外的所有子目录和文件 - Remove all sub-directories and files except for one 如何在不chmod文件夹的情况下chmod子目录中的所有文件? - How to chmod all files in sub-directories without chmod the folders? 如何使用 PowerShell 递归搜索目录和子目录中的所有文件? - How to recursively search all files in a directory and sub-directories using PowerShell? 如何解压缩子目录中的所有文件? - How to unzip all files in sub-directories? 在没有dos2unix的情况下递归转换目录和子目录中所有文件的所有EOL(dos-> unix) - Convert all EOL (dos->unix) of all files in a directory and sub-directories recursively without dos2unix 如何将所有子目录中的所有文件gzip压缩为bash中的一个压缩文件 - How to gzip all files in all sub-directories into one compressed file in bash LINUX CP:我需要将所有文件从一个目录复制到另一个目录而不复制任何子目录 - LINUX CP: I need to copy all files from one directory to another without copying any sub-directories 对于所有子目录,将目录中指定扩展名的所有文件递归转换为pdf - Convert all files of a specified extension within a directory to pdf, recursively for all sub-directories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM