Linux shell脚本练习(三)
获取某个目录下的文件数量
TARGET_DIR="/opt/test/"
count=0
if [ -d "$TARGET_DIR" ]; then
files=$(find "$TARGET_DIR" -type f)
for file in $files; do
count=$(( $count + 1 ))
done
echo "Total number of files in '$TARGET_DIR': $count" > record.txt
else
echo "Directory '$TARGET_DIR' does not exist."
fi
[root@iZ2vcgxbgi6b6kbx279yozZ test]# chmod +x count_file.sh
[root@iZ2vcgxbgi6b6kbx279yozZ test]# ./count_file.sh
[root@iZ2vcgxbgi6b6kbx279yozZ test]# ls
aa.txt bb.txt cc.txt count_file.sh record.txt
[root@iZ2vcgxbgi6b6kbx279yozZ test]# cat record.txt
Total number of files in '/opt/test/': 4
获取某目录下的文件数量,包括子文件数量
if [ -z "$1" ]; then
echo "Usage: $0 <directory>"
exit 1
fi
DIR="$1"
file_count=$(find "$DIR" -type f | wc -l)
echo "Total number of files in directory '$DIR' and its subdirectories: $file_count"
原文地址:https://blog.csdn.net/m0_54103484/article/details/144435882
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!