在不查找的情况下查找并删除目录及其子目录中的空文件 [英] find and delete empty files in directory and its subdirs without find

查看:37
本文介绍了在不查找的情况下查找并删除目录及其子目录中的空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 bash 脚本,用于在不使用 find 命令的情况下查找和删除目录(包括子目录)中的空文件.

I am trying to make a bash script that finds and removes empty files in a directory including subdirectories, without using the find command.

这是使用 find 命令的脚本的一部分,但我不确定如何在不使用 find 的情况下转换此行.

This is part of the script using the find command but I am unsure how to convert this line without using find.

找到.-type f -empty -delete

推荐答案

试试这个代码:

# enable recursive globstar matching
shopt -s globstar

# directory to delete files from
dir="/tmp"

# loop through files recusively
for f in ${dir}/* ${dir}/**/* ; do
  # check if file is empty
  if [ ! -s "$f" ]; then
    # remove file
    rm "$f"
  fi
done

这篇关于在不查找的情况下查找并删除目录及其子目录中的空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆