How to print full path name along with file extension?

Hi

I have a requirement like this:

/abc/a/x.txt
/abc/a/y.txt
/abc/b/x.gz
/abc/b/y.txt

I need output like this:

/abc/a:*.txt
/abc/b:*.txt
/abc/b:*.gz

I have tried

 find /abc -type f -name "*.*" ||awk -F . '{print $NF}'  

it is print only extensions without path name.

Please advice!

Thanks

Try something like this to start with. If you find problems, show us what you did to fix them. First. Before saying 'this does not work':

find /abc -type d |
while read dir
do
   ls -1 /abc/$dir | awk -F '.' '{printf $(NF)' | sort -u |
   while read suffix
   do
      printf "/abc/%s:*.%s"  $dir $suffix
   done
done

Comment: this appears to be an attempt to do something else, and this is a problem you hit on the way to that something else's solution. It is an unusual request.
Or:
Is this homework?

1 Like
find /abc -type f -printf "%h %f\n" | awk '
        {
                EX = $2
                sub ( /[^.]*[.]/, X, EX )
                A[$1 FS EX]
        }
        END {
                for ( k in A )
                {
                        n = split ( k, T )
                        print T[1] "/:*." T[2]
                }
        }
'
1 Like

Try

find /abc | awk '{sub (/[^\/]*\./, "*."); T[$0]} END {for (t in T) print t}'
/abc/b/*.gz
/abc/b/*.txt
/abc/a/*.txt
1 Like

Even this could do the trick:-

#!/bin/bash

while read full_filename
do
   directory="${full_filename%/*}/"
   filenames="*.${full_filename##*.}"
   echo "${directory}${filenames}"
done < <(find /abc -type f) | sort -u

I'm not saying it's pretty; it is just another alternative if that suits your coding style. It uses very few processes, which might be important to performance if you have lots of files.

Assuming that this is to be used by something else that will expand it all again, is there a reason not to build this process into that code and remove the need to juggle it all and then re-expand?

Can you elaborate?

Robin

1 Like

Thank you all.
This:

find /abc | awk '{sub (/[^\/]*\./, "*."); T[$0]} END {for (t in T) print t}'

worked for me.

It is not homework :slight_smile:
I have to prepare a list with all sub directories and with file extensions in all .