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 1 Week Ago   -   Original Discussion by lijjumathew
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; 1 Week Ago at 05:28 PM..
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by lijjumathew
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 November 2017, 2:26 PM EST
Location: NM
Posts: 11,240
Thanks: 571
Thanked 1,117 Times in 1,031 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 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week 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: 22 November 2017, 7:44 PM EST
Location: Galactic Empire
Posts: 3,603
Thanks: 253
Thanked 1,316 Times in 1,233 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 (1 Week Ago)
    #4  
Old Unix and Linux 1 Week Ago   -   Original Discussion by lijjumathew
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:45 PM EST
Location: Aachen, Germany
Posts: 11,644
Thanks: 321
Thanked 3,619 Times in 3,324 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 (1 Week Ago)
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago   -   Original Discussion by lijjumathew
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 21 November 2017, 12:04 PM EST
Location: Lancashire, UK
Posts: 3,373
Thanks: 1,456
Thanked 665 Times in 598 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; 1 Week Ago at 09:18 AM.. Reason: Smartened code
The Following User Says Thank You to rbatte1 For This Useful Post:
lijjumathew (1 Week Ago)
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago   -   Original Discussion by lijjumathew
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; 1 Week 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 01:38 AM.