Reputation: 984
As in the title, can someone help to answer this only using the Bash?
I spent some time can't figure it out. Thanks.
Upvotes: 1
Views: 235
Reputation: 75478
This would list those directories without being redundant on searches.
#!/bin/bash
function check {
local DIR SUBFOLDERS=()
for DIR; do
readarray -t SUBFOLDERS < <(find "$DIR" -maxdepth 1 -mindepth 1 -type d)
if [[ ${#SUBFOLDERS[@]} -gt 0 ]]; then
[[ ${#SUBFOLDERS[@]} -eq 2 ]] && echo "$DIR"
check "${SUBFOLDERS[@]}"
fi
done
}
check "$@"
Run the script with
bash script.sh dir_to_search [optionally_another_dir_to_search ...]
Examples:
bash script.sh .
bash script.sh "$HOME"
bash script.sh /var /usr
Upvotes: 1
Reputation: 123458
The following should list the directories containing only two folders folder1
and folder2
.
for i in $(find . -type d); do count=$(find $i -mindepth 1 -maxdepth 1 -type d | wc -l); [ $count -eq 2 ] && [ -d $i/folder1 ] && [ -d $i/folder2 ] && echo $i ; done
The above wouldn't care if the folders have other files. If you want to ensure only directories folder1
and folder2
, say:
for i in $(find . -type d); do count=$(find $i -mindepth 1 -maxdepth 1 | wc -l); [ $count -eq 2 ] && [ -d $i/folder1 ] && [ -d $i/folder2 ] && echo $i ; done
[Be warned that the above might not work with spaces in directory names.]
Upvotes: 3