Issue
i have folders inside input_scan but i need folder1 and folder3 beacuse they are super folders for 5.txt,1.txt and 2.txt. how can i get this? with the current code i am getting folder1 for 5.txt, folder2 for 1.txt and folder7 for 2.txt
Ex:
input_scan
/folder1
/5.txt
/folder2
/1.txt
/folder3
/folder4
/folder5
/folder6
/folder7
/2.txt
while IFS= read -r -d '' file; do
if [ -f "$file" ]; then
parent_dir=$(basename "$(dirname "$file")")
echo $parent_dir
fi
done < <(find "input_scan" -type f -print0)
Solution
With GNU utils.
find "input_scan" -type f -print0 | cut -zd'/' -f2 | sort -zu
If it is a shell loop.
#!/usr/bin/env bash
declare -A uniq
while IFS= read -rd '' file; do
dir=${file#*/}
((uniq["${dir%%/*}"]++))
done < <(find "input_scan" -type f -print0)
printf '%s\n' "${!uniq[@]}"
Since there is no need to count the directories, we can change
((uniq["${dir%%/*}"]++))
To:
uniq["${dir%%/*}"]=
Answered By - Jetchisel Answer Checked By - Candace Johnson (WPSolving Volunteer)