BASH:编写一个脚本递归地遍历N个级别的目录

BASH:编写一个脚本递归地遍历N个级别的目录,bash,shell,recursion,Bash,Shell,Recursion,例如,我有以下目录结构: /test_dir/d /test_dir/d/cron /test_dir/d/cache /test_dir/d/...(more sub dirs) /test_dir/tree /test_dir/tree/a /test_dir/tree/a/a1 /test_dir/tree/a/a2 ...(and so on for b/ and c/ ) 我已经编写了下面的bash脚本,它有效地移动到了/test\u dir的第二级,因此它将到达/test\u di

例如,我有以下目录结构:

/test_dir/d
/test_dir/d/cron
/test_dir/d/cache
/test_dir/d/...(more sub dirs)
/test_dir/tree
/test_dir/tree/a
/test_dir/tree/a/a1
/test_dir/tree/a/a2
...(and so on for b/ and c/ )
我已经编写了下面的bash脚本,它有效地移动到了
/test\u dir
的第二级,因此它将到达
/test\u dir/d/cron
/test\u dir/tree/a
,但不会进一步。我不明白为什么递归脚本不会进一步运行。有人能调试脚本并指出我的错误吗

以下是我写的:

#!/bin/bash

#script to recursively travel a dir of n levels

function traverse() {   

for file in `ls $1`
do
    #current=${1}{$file}
    if [ ! -d ${1}${file} ] ; then
        echo " ${1}${file} is a file"
    else
        #echo "entering recursion with: ${1}${file}"
            traverse "${1}/${file}"
    fi
done
}

function main() {
    traverse $1
}

main $1
以下是输出:

/test_dir/a is a file
/test_dir/b is a file
/test_dir//dcache is a file
/test_dir//dcron is a file
/test_dir//dgames is a file
/test_dir//dlib is a file
/test_dir//dlog is a file
/test_dir//drun is a file
/test_dir//dtmp is a file
/test_dir/movies is a file
/test_dir//treea is a file
/test_dir//treeb is a file
/test_dir//treec is a file
/test_dir//treed is a file
我知道可能有更优雅的单行命令来实现这一点。但我试图以这种明确的方式来做。我为这篇文章的篇幅道歉

编辑:使用
遍历“${1}/${file}”

这些都是错误。一些不仅不是目录,而且不存在的文件被视为文件。我认为您需要用斜杠将新文件与目录分开:

    traverse "${1}/${file}"
还要检查它们是否也存在

既然您也在使用bash,我建议使用以下表单:

#!/bin/bash

shopt -s dotglob  ## Optionally would allow matches for directories beginning with .
shopt -s nullglob

function traverse {
    local a file
    for a; do
        for file in "$a"/*; do
            if [[ -d $file ]]; then
                traverse "$file"
            else
                echo " $file is a file."
            fi
        done
    done
}

traverse "$@"
可以使用多个参数运行脚本

脚本的固定版本:

#!/bin/bash

#script to recursively travel a dir of n levels

function traverse() {   
    for file in $(ls "$1")
    do
        #current=${1}{$file}
        if [[ ! -d ${1}/${file} ]]; then
            echo " ${1}/${file} is a file"
        else
            #echo "entering recursion with: ${1}${file}"
            traverse "${1}/${file}"
        fi
    done
}

function main() {
    traverse "$1"
}

main "$1"

脚本有几个问题。应该是这样的:

#!/bin/bash

#script to recursively travel a dir of n levels

function traverse() {
for file in "$1"/*
do
    if [ ! -d "${file}" ] ; then
        echo "${file} is a file"
    else
        echo "entering recursion with: ${file}"
        traverse "${file}"
    fi
done
}

function main() {
    traverse "$1"
}

main "$1"
但是,递归遍历目录的正确方法是使用find命令:

find . -print0 | while IFS= read -r -d '' file
do 
    echo "$file"
done

非常感谢。我试过你的建议,但它仍然在做同样的事情。我将编辑帖子,以便您可以看到更改。您还应该在检查中添加:
if[!-d${1}/${file}];然后
在下一行
echo“${1}/${file}是一个文件”
。不能用“-f”代替“-d'?您正在尝试检测文件,对吗?@XChikuX正确,该部分是从OP的代码复制的
find . -print0 | while IFS= read -r -d '' file
do 
    echo "$file"
done