简体   繁体   English

如何修复此 Bash 函数以从指定路径开始并递归列出其子目录和这些子目录等

[英]How can I fix this Bash function to start at a specified path and recursively list its subdirectories and the subdirectories of those etc

I am trying to write a script that searches a directory and it's subdirectories etc for files matching a given regular expression.我正在尝试编写一个脚本来搜索目录及其子目录等以查找与给定正则表达式匹配的文件。 So I have started by attempting to write a function to get the directories and subdirectories first.所以我开始尝试编写一个函数来首先获取目录和子目录。 For some reason it currently only seems to get the first subdirectory in the specified directory.出于某种原因,它目前似乎只获取指定目录中的第一个子目录。

Here is the function:这是函数:

getDirs() {

cd "$1"
for i in *; do
    if [ -d "$i" ]; then
        echo "dir: $PWD/$i"
        getDirs "$i"
    fi
done
}

getDirs $path

Any ideas how to fix this?任何想法如何解决这一问题?

Try doing this using if you need a regex for searching file names :如果您需要正则表达式来搜索文件名,请尝试使用执行此操作:

regex="YourRegexPattern"
find "$1" -type f -regextype posix-egrep -regex "$regex"

if you want to get all dirs/subdirs :如果您想获取所有目录/子目录:

find . -type d

This should do it, although find is more efficient.这应该可以做到,尽管find更有效。

getDirs() {
    for i in "$1"/*; do
        if [ -d "$i" ]; then
            echo "$i"
            getDirs "$i"
        fi
    done
}

Sure, that's only because you never go back to the previous directory after your loop.当然,那只是因为您在循环后永远不会回到上一个目录。 You could put everything in a subshell thus:你可以把所有东西都放在一个子shell中:

getDirs() {
    (
    cd "$1"
    for i in *; do
        if [[ -d "$i" ]]; then
            echo "dir: $PWD/$i"
            getDirs "$i"
        fi
    done
    )
}

getDirs $path

or save the current dir to cd to it after your loop thus:或者在循环后将当前目录保存到cd到它,因此:

getDirs() {
    local currentdir=$PWD
    cd "$1"
    for i in *; do
        if [[ -d "$i" ]]; then
            echo "dir: $PWD/$i"
            getDirs "$i"
        fi
    done
    cd "$currentdir"
}

getDirs $path

or other... I guess you now see where your mistake was!或者其他......我想你现在知道你的错误在哪里了!

You should also check that your cd worked with, eg, cd "$1" || <do something as the cd failed>您还应该检查您的cd是否可以使用,例如cd "$1" || <do something as the cd failed> cd "$1" || <do something as the cd failed> . cd "$1" || <do something as the cd failed>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何递归grep并忽略子目录的子目录? - How to grep recursively and ignore subdirectories of subdirectories? 如何编写bash脚本以递归方式扫描子目录并列出当前月份的文件? - How to write bash script which recursively scan subdirectories and list files with current day of month? 如何递归查找并列出具有子目录和时间的目录中的最新修改文件 - How to recursively find and list the latest modified files in a directory with subdirectories and times 如何在 C 中使用 scandir() 递归地列出排序的子目录 - How to use scandir() in C to list sorted subdirectories recursively 如何将目录中的大量zip文件移动到bash中指定数量的多个子目录中? - How do I move a large number of zip files in a directory to a specified number of multiple subdirectories in bash? 我如何递归grep所有目录和子目录? - How do I recursively grep all directories and subdirectories? Bash - 如何在子目录中存档和压缩文件,但只能使用特定的文件名 - Bash - How can I archive and compress files in subdirectories but only with a certain filename 如何在Linux中递归重命名目录和子目录? - How to rename directory and subdirectories recursively in linux? 如何在我的Makefile中包含目录及其所有子目录的标题? - How can I include headers from a directory and all its subdirectories in my makefile? 列出具有指定深度的文件夹的所有子目录 - List all subdirectories of a folder with with an specified depth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM