![](/img/trans.png)
[英]Writing a bash script to rename files sequentially in current directory and sub-directories
[英]Writing a Bash script to remove all zero length ordinary files in the directory and all sub-directories without using -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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.