With awk
:
find . | awk -F / ' {t[$NF]=t[$NF] $0 ORS; c[$NF]++} END {for (k in t) if (c[k]>1) printf "%s", t[k]}'
Notes:
The above code processes pathnames as newline-terminated strings; pathnames with newline characters will break it.
- To reliably handle pathnames containing newline characters, process them as null-terminated strings. To do so, change the first line of our code to
find . -print0 | awk -v RS='\0' -F / '
(not portable).awk
will still output newline-terminated strings (useful for viewing), unless…To make
awk
output null-terminated strings (useful for further reliable processing), add-v ORS='\0'
to its options. The first line of the code will be:find . -print0 | awk -v RS='\0' -v ORS='\0' -F / '
- To reliably handle pathnames containing newline characters, process them as null-terminated strings. To do so, change the first line of our code to
To terminate each group of pathnames with an empty line, change
printf "%s", t[k]
toprintf "%s", t[k] ORS
.Parsing the output of
ls
(like you did) is not recommended. Parsing the output offind
(like we did) is fine (with the caveat of newlines in pathnames).