Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 645

Answer by Kamil Maciorowski for display path to files with same names

$
0
0

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 terminate each group of pathnames with an empty line, change printf "%s", t[k] to printf "%s", t[k] ORS.

  • Parsing the output of ls (like you did) is not recommended. Parsing the output of find (like we did) is fine (with the caveat of newlines in pathnames).


Viewing all articles
Browse latest Browse all 645

Trending Articles