How to recursively list subdirectories in Bash without using "find" or "ls" commands?


Question

I know you can use the find command for this simple job, but I got an assignment not to use find or ls and do the job. How can I do that?

1
11
9/18/2018 5:43:02 PM

you can do it with just the shell

#!/bin/bash
recurse() {
 for i in "$1"/*;do
    if [ -d "$i" ];then
        echo "dir: $i"
        recurse "$i"
    elif [ -f "$i" ]; then
        echo "file: $i"
    fi
 done
}

recurse /path

OR if you have bash 4.0

#!/bin/bash
shopt -s globstar
for file in /path/**
do
    echo $file
done
30
1/28/2010 12:33:00 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon