Home > Blockchain >  How to loop through a directory and all of its sub-directories?
How to loop through a directory and all of its sub-directories?

Time:03-26

I would like to write a bash script, that loops through all sub-directories of an input directory. Here, the $1 would be a directory name. I would like my script to loop through every single sub-directory (and their sub-directories and so on)

A sample tree would be something like:


parent
├── fold1
│   ├── amsda
│   │   ├── auojdand
│   │   └── auojdand2
│   ├── txtfil
│   └── txtfil2
├── fold2
│   ├── fold2txt
│   ├── fold2txt2
│   └── fold2txt3
├── fold3
│   ├── fold3txt
│   └── fold3txt2
├── txtfileparentben2
└── txtfilparentben

I would like to check every textfile in the "parent" directory (and its sub-directories), but that is a question for later.

CodePudding user response:

This command will give you all the directories within parent

find /parent -type d

use man find for description of the command and all available options

  • Related