0

I'm writing a bash script that needs to list all the names of folders inside a certain folder and print their description from a file inside each folder. here is a example filesystem:

main
||
\/
folder1
folder2
folder3
folder4
template

inside each folder other than main there is a file called description (including the template folder), here is an example description file:

folder1
=======
contains:
* x
* y
* z

the folders might change name, more be added, and some removed. so I don't want to hardcode any number or name

I want to print all the folders and their descriptions like this using only bash and as little if any other programs (cat, grep etc. is ok) and without the template folder:

**folder1**
folder1
=======
contains:
* x
* y
* z

**folder2**
folder2
=======
contains:
* x
* y
* z

**folder3**
folder3
=======
contains:
* x
* y
* z

**folder4**
folder4
=======
contains:
* x
* y
* z

^note that the template folder isn't in my example output of what I want

so far I have this code:

function list-all() {
  folderlist=()
  for file in /home/USER/main/*; do
      echo "$(basename "$file")"
  done
}

it prints all the folder names in a vertical list like this:

folder1
folder2
folder3
folder4
template

from this output I want to get to this:

folder1
folder1
=======
contains:
* x
* y
* z

folder2
folder2
=======
contains:
* x
* y
* z

folder3
folder3
=======
contains:
* x
* y
* z

folder4
folder4
=======
contains:
* x
* y
* z

thanks a lot in advance!

5
  • 2
    Do you simply mean cat main/*/description?
    – terdon
    Mar 6 at 18:12
  • @terdon I also want the folder name and I want to omit the template folder. see my example output. Mar 6 at 18:29
  • According to the question, the folder name is in the description file, and the description file does not contain the description of template. Of course, you might argue that cat is not Bash. Mar 6 at 18:33
  • @NasirRiley I edited my question and added what I tried. Mar 6 at 18:34
  • @berndbausch thats not what I mean. I edited my question to make it clearer. Mar 6 at 18:38
2

I think what you are going for is something like this:

for dir in /home/USER/main/*/; do
    dirname=$(basename "$dir")
    if [[ "$dirname" != "template" ]]; then        
        echo "$dirname"
        cat "$dir"/description
    fi
done
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.