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?

👤 Login to reply

    #1  
Old 11-14-2017
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; 11-14-2017 at 04:28 PM..
Sponsored Links
    #2  
Old 11-14-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 21 July 2018, 12:51 AM EDT
Location: NM
Posts: 11,441
Thanks: 638
Thanked 1,176 Times in 1,082 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 (11-15-2017)
Sponsored Links
    #3  
Old 11-14-2017
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 20 July 2018, 11:42 PM EDT
Location: Galactic Empire
Posts: 3,671
Thanks: 257
Thanked 1,331 Times in 1,247 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 (11-15-2017)
    #4  
Old 11-15-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 July 2018, 2:56 AM EDT
Location: Aachen, Germany
Posts: 13,081
Thanks: 452
Thanked 4,017 Times in 3,693 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 (11-15-2017)
Sponsored Links
    #5  
Old 11-15-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 July 2018, 11:57 AM EDT
Location: Lancashire, UK
Posts: 3,588
Thanks: 1,594
Thanked 706 Times in 633 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; 11-15-2017 at 08:18 AM.. Reason: Smartened code
The Following User Says Thank You to rbatte1 For This Useful Post:
lijjumathew (11-15-2017)
Sponsored Links
    #6  
Old 11-15-2017
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
I have to prepare a list with all sub directories and with file extensions in all .

Last edited by Scott; 11-15-2017 at 12:00 PM.. Reason: C'mon, people, code tags, please! It's not that hard to do!
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 04:08 AM
Retrieve directory path from full file path through sh royzlife Shell Programming and Scripting 7 09-21-2010 08:28 AM
list file with full path ryandegreat25 Shell Programming and Scripting 2 07-13-2009 02:11 AM
to find the file with full path surjyap Shell Programming and Scripting 5 01-18-2008 02:26 PM
process executable file full path xtrix UNIX for Advanced & Expert Users 3 10-11-2004 04:28 AM



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

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?