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?
Linux recursive directory listing using ls -R command. The -R option passed to the ls command to list subdirectories recursively.
By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.
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
Try using
tree -d
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With