昊鼎王五:shell:遍历目录和子目录的所有文件

版权声明:感谢您阅读我的文章,转载注明出处哦~~ https://blog.csdn.net/haoding205/article/details/82112551

昊鼎王五:shell:遍历目录和子目录的所有文件

代码如下:

#!/bin/bash
function getdir(){
    for element in `ls $1`
    do  
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ]
        then 
            getdir $dir_or_file
        else
            echo $dir_or_file
        fi  
    done
}
root_dir="/haoding/hd205"
getdir $root_dir

备注:
以下命令均不包含”.”,”..”目录,以及”.”开头的隐藏文件,如需包含,ll 需要加上 -a参数

ll |grep "^-"|wc -l    #当前目录下文件个数,不包含子目录
ll |grep "^d"|wc -l    #当前目录下目录个数,不包含子目录
ll -R|grep "^-"|wc -l   #当前目录下文件个数,包含子目录
ll -R|grep "^d"|wc -l    #当前目录下目录个数,包含子目录

好了,聪明如你,知道了遍历目录和子目录的所有文件的shell,是不是很欢喜 ^_^

还有其他问题的可以在评论区留言或者扫码加博主获取资源或者提问。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/haoding205/article/details/82112551
今日推荐