如何解决编写一个Bash脚本以删除目录和所有子目录中所有零长度的普通文件,而无需使用-find
这就是我所拥有的,但是我很确定这只会删除目录及其直接子目录中的文件。我将如何定位所有子目录?
#!/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
解决方法
您可以发出:
find /path/to/your/directory -size 0 -print -delete
这将删除目录(及以下)中大小为零的所有文件。
,请您尝试以下操作:
#!/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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。