功能:统计(参数)路径下每个文件大小,记录属主,并且不统计超链接的文件和文件夹。
#!/bin/bash
# 定义函数来递归遍历文件夹
function traverse_directory {
local directory="$1"
# 遍历目录下的文件和子目录
for item in "$directory"/*; do
# 检查是否为符号链接,如果是则跳过
if [ -L "$item" ]; then
continue
fi
# 检查是否为文件
if [ -f "$item" ]; then
# 获取文件属主
owner=$(stat -c "%U" "$item")
# 获取文件大小(以字节为单位)
size=$(stat -c "%s" "$item")
# 获取文件路径
filepath="$item"
# 输出统计结果
echo "$owner $size $filepath"
elif [ -d "$item" ]; then
# 如果是目录,递归遍历
traverse_directory "$item"
fi
done
}
# 检查是否提供了目录参数
if [ $# -ne 1 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# 获取传入的目录参数
directory="$1"
# 检查目录是否存在
if [ ! -d "$directory" ]; then
echo "Directory not found: $directory"
exit 1
fi
# 调用遍历函数
traverse_directory "$directory"