Bash script recursive file list

In this tutorial, How do I use BASH script to recursively list files directories and subdirectories? Bash script the essential for DevOps Roles. Bash script recursive file list example.

Example List all files in directories and files in subdirectories as below

[huupv@huupv devopsroles]$ pwd
[huupv@huupv devopsroles]$ tree /home/huupv/devopsroles
├── file.txt
├── folder
│   └── file.txt
├── link_folder -> /home/huupv/devopsroles/folder

2 directories, 12 files

My Bash script recursive file list

# Colourise the output on terminal
RED='\033[0;31m' # Red
GRE='\033[0;32m' # Green
NCL='\033[0m' # No Color

print_file_recursively() {
   FILE_NAME="$(basename "${entry}")"
   printf "%*s\t[FILE]:\t${GRE} $FILE_NAME %s${NCL}\n"

# loop and print all file and folder recusively,
print_recursively() {
   local indent="${2:-0}"
   printf "\n%*s${RED}%s${NCL}\n\n" "$indent" '[DIR]: ' "$1"
for entry in "$1"/*; do
   [[ -f "$entry" ]] && print_file_recursively
for entry in "$1"/*; do
   [[ -d "$entry" ]] && print_recursively "$entry" $((indent+4))

# Check folder
if [ -d "$1" ]; then
print_recursively $PATH_FOLDER

The screen output terminal:

Bash script recursive file list


Thought the article, you can use Bash script recursive file list as above. I hope will this your helpful. More details refer to Bash script.


About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.