Reputation: 1289
I am trying to simply trying to determine how many non empty folders are present in each of the base directories & ignore any file directly in the root directory.
To explain it better, here's the tree structure
❯ tree $HOME/count
/home/xd003/count
├── Random file in root to be ignored.mp4
├── base_folder1
│ ├── Empty folder to be ignored
│ └── Folder 1
│ └── file.mp4
└── base_folder2
├── Folder 1
│ └── file.mp4
└── Folder 2
└── file.mp4
The 1st file needs to be ignored as it's directly in root. base_folder1
has only 1 non empty folder ('Folder1') while base_folder2
has 2 non empty folders ('Folder 1' & 'Folder 2')
Here's my bash script to determine the same.
foldercount() {
cd $HOME/count
for dir in *; do
local name="${dir}"
[[ -f ${name} ]] && continue #skip the files in root
local folders total_folders top=0
mapfile -t folders <<< "$(find "${name}" -type f -printf "%h\n" | sort -uV)"
total_folders="${#folders[@]}"
if [[ -z ${folders} ]]; then
continue #skip empty folders
elif [[ "${total_folders}" -ge 1 ]]; then
echo "More than 1 non empty folders are detected"
else
echo "Only 1 non empty folder is detected"
fi
done
}
foldercount
The issue is that it prints more than 1 non empty folder is detected for both of the base folders. The else condition simply doesn't gets executed. Ideally the else condition should get executed for base_folder1
PS: I need to fix the issue in my current script, I can't use a alternative solution as this is a small portion of a large script.
Upvotes: 0
Views: 101
Reputation: 1289
As pointed out in the comments
The operator -ge is for greater than or equal to. Using the correct operator as shown below fixes the issue -
elif [[ "$total_folders" -gt 1 ]]
or
elif (( $total_folders > 1 ))
Upvotes: 1