shell遍历文件性能ls -R / tree

本文介绍了一个用于统计指定目录下所有文件及子目录中文件数量的shell脚本。该脚本通过递归方式读取目录,并使用ls命令按不同排序方式列出文件详情,最后输出文件总数和目录总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fs_file_list.sh


#!/system/bin/sh
#########  #! /bin/bash
test_dir=$1

function read_dir(){
    for file in `ls $1`
    do
        if [ -d $1"/"$file ]
        then
            read_dir $1"/"$file
        else
            echo $1"/"$file
        fi
    done
}

start=$(date +%s) #.%N)
#scan by ls -R
ls -l -R $test_dir
ls -l -R -r $test_dir
ls -l -R -S $test_dir
ls -l -R -t $test_dir
#ls -l -R -X $test_dir
tree  ## tree source code : http://mama.indstate.edu/users/ice/tree/
#lsof 
end=$(date +%s) #.%N)

file=$(ls -l -R $test_dir |grep "^-"|wc -l) 
dir=$(ls -l -R $test_dir |grep "^d"|wc -l)

echo "file nums:$file"
echo "dir num:$dir"

#echo $start 
#echo $end
time1=$(($end-$start))

#scan by ls
#start=$(date +%s) #.%N)
#read_dir $test_dir
#end=$(date +%s) #.%N)
#time2=$(($end-$start))

echo "ls time:$time1 s"
#echo "$time2 s"

如果需要统计文件数和目录数,可以参考如下命令:
ls -l * |grep "^-"|wc -l ---文件个数  
ls -l * |grep "^d"|wc -l    ---目录个数




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值