Unix/Linux Go Back    


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

How to print full path name along with file extension?

Linux


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by lijjumathew
lijjumathew's Unix or Linux Image
lijjumathew lijjumathew is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 15 November 2017, 11:58 AM EST
Posts: 18
Thanks: 15
Thanked 0 Times in 0 Posts
How to print full path name along with file extension?

Hi

I have a requirement like this:

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

I need output like this:

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

I have tried
Code:
 find /abc -type f -name "*.*" ||awk -F . '{print $NF}'

it is print only extensions without path name.

Please advice!

Thanks

Last edited by jim mcnamara; 4 Weeks Ago at 05:28 PM..
Sponsored Links
    #2  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by lijjumathew
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 17 December 2017, 10:01 PM EST
Location: NM
Posts: 11,256
Thanks: 578
Thanked 1,122 Times in 1,034 Posts
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':


Code:
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?
The Following User Says Thank You to jim mcnamara For This Useful Post:
lijjumathew (4 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by lijjumathew
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 December 2017, 1:34 PM EST
Location: Galactic Empire
Posts: 3,613
Thanks: 254
Thanked 1,317 Times in 1,234 Posts

Code:
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]
                }
        }
'

The Following User Says Thank You to Yoda For This Useful Post:
lijjumathew (4 Weeks Ago)
    #4  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by lijjumathew
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 December 2017, 2:31 PM EST
Location: Aachen, Germany
Posts: 11,817
Thanks: 342
Thanked 3,659 Times in 3,357 Posts
Try

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

The Following User Says Thank You to RudiC For This Useful Post:
lijjumathew (4 Weeks Ago)
Sponsored Links
    #5  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by lijjumathew
rbatte1's Unix or Linux Image
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 15 December 2017, 1:10 PM EST
Location: Lancashire, UK
Posts: 3,411
Thanks: 1,477
Thanked 669 Times in 602 Posts
Bash variable substitution option

Even this could do the trick:-
Code:
#!/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

Last edited by rbatte1; 4 Weeks Ago at 09:18 AM.. Reason: Smartened code
The Following User Says Thank You to rbatte1 For This Useful Post:
lijjumathew (4 Weeks Ago)
Sponsored Links
    #6  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by lijjumathew
lijjumathew's Unix or Linux Image
lijjumathew lijjumathew is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 15 November 2017, 11:58 AM EST
Posts: 18
Thanks: 15
Thanked 0 Times in 0 Posts
Thank you all.
This:

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

worked for me.

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

Last edited by Scott; 4 Weeks Ago at 01:00 PM.. Reason: C'mon, people, code tags, please! It's not that hard to do!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract strings from full path when full path is not fixed Shirisha Shell Programming and Scripting 0 10-07-2010 05:08 AM
Retrieve directory path from full file path through sh royzlife Shell Programming and Scripting 7 09-21-2010 09:28 AM
list file with full path ryandegreat25 Shell Programming and Scripting 2 07-13-2009 03:11 AM
to find the file with full path surjyap Shell Programming and Scripting 5 01-18-2008 03:26 PM
process executable file full path xtrix UNIX for Advanced & Expert Users 3 10-11-2004 05:28 AM



All times are GMT -4. The time now is 03:07 AM.