Finding a subfolder with space in a folder name using BASH

I am writing a shell script that iterates through a set of IMAP folders.

Given:

WORKPATH=/var/mail/mydomain.com/myaccount/ 

I want to list all the subfolders inside.

Easy to find:

 for SUB_FOLDER in $(find "$WORKPATH" -type d) do echo "SUB_FOLDER: $SUB_FOLDER" done 

However: Some of these folders have a place in their name, for example:

 /var/mail/mydomain.com/myaccount/.Deleted Mails/ /var/mail/mydomain.com/myaccount/.Junk Mails/ 

What to do on the code above to print something in the neighborhood:

 SUB_FOLDER: /var/mail/mydomain.com/myaccount/.Deleted SUB_FOLDER: Mails/ SUB_FOLDER: /var/mail/mydomain.com/myaccount/.Junk SUB_FOLDER: Mails/ 

I tried using the -print0 and pipe option for xargs -0, but I had no luck. Playing with IFS has done even worse things for output than you need.

Could really use a hint, as make find makes only 2 tokens to increment a for 4 loop or an alternative way to iterate over a list.

+4
source share
1 answer

Use the while :

 while read SUB_FOLDER; do echo "SUB_FOLDER: $SUB_FOLDER"; done < <(find "$WORKPATH" -type d) 
+6
source

Source: https://habr.com/ru/post/1491341/


All Articles