Sorting a list of filenames but keeping the path information.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting a list of filenames but keeping the path information.
# 1  
Old 06-23-2011
Sorting a list of filenames but keeping the path information.

Hi All

I've googled around for this and can't see a way of doing it.

I have a file that contains a number of records that are layed out something like the following.

/path/to/directory/that/contains/a/file/I/need/filename.pdf

The path itself can vary both in terms of the names and the number of subdirectories before I get to the file.

I realise I can use basename to extract the filename and do a sort but what I want to do is to sort the list using the filename as the key and then find duplicate filenames and then identify the paths of the conflicting filename.

I also want to be able to do a quick and dirty compare from the command line something like

Code:
cat /the/input/file | sed -e 's\: delete up to the last forward slash in the path to leave just the filename ::' | sort | uniq

To allow me to do a fast visual confirmation on if there are duplicates in the file. I can't see a way to get sed to delete up to the last instance of a specific character on a line (basename won't work from the command line as there's multiple records in the input file so it needs a loop).

All ideas gratefully received.
# 2  
Old 06-23-2011
This might help:

code:
echo "/path/to/directory/that/contains/a/file/I/need/filename.pdf" | awk -F"/" ' { print $NF } '

Use this awk syntax for the rest of the lines.
This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 06-23-2011
Quote:
Originally Posted by jayan_jay
This might help:

code:
echo "/path/to/directory/that/contains/a/file/I/need/filename.pdf" | awk -F"/" ' { print $NF } '

Use this awk syntax for the rest of the lines.
Thanks, what's the $NF variable? I don't think I've ever used it before.

edit
Ignore, I've just seen it's the number of fields and therefore by definition the last field, nice idea, thanks.
# 4  
Old 06-23-2011
an awk one:

Code:
 
awk -F"/" '{ a[$NF]++;b[$NF]=$0"\n"b[$NF]} END{ for(i in a ) if (a[i]>1) print i"-->",a[i],"\n"b[i]}' input_file

Not sure of your requirement but I hope the above helps!
# 5  
Old 06-23-2011
Code:
while read rec
do
  echo "$(basename $rec)  $rec"
done < listfile | sort | awk '{print $2}'

start with that
# 6  
Old 06-23-2011
yet another [g]awker...
Code:
awk -F/ '{f[$NF]=sprintf("%s",f[$NF]?f[$NF]"\n"$0:$0)}END{for(i in f) print f[i]}' file

# 7  
Old 06-23-2011
Quote:
Originally Posted by jayan_jay
This might help:

code:
echo "/path/to/directory/that/contains/a/file/I/need/filename.pdf" | awk -F"/" ' { print $NF } '

Use this awk syntax for the rest of the lines.
Jay

Just wanted a further follow up to thank you for this.

I was using basename a couple of other places in the script and replacing with this is going to strip about an hour off the script runtime.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting using filename of a path

Hi all! i have a question how do you sort the filename of a path directory according to alphabetic order. Example: sort according to highlighted text. There maybe space for filename Path=/home/pikamon/Desktop/ABC; Path=/home/pikamon/Desktop/ABD; Path=/home/pikamon/Desktop/Riduan la;... (5 Replies)
Discussion started by: pikamon
5 Replies

2. Shell Programming and Scripting

Sorting group information for accounts

I have an input file that contains the primary and secondary groups a user should have based on a pre-defined role. The input file looks like this: <user_login>|<comment_field>|<role> After I execute my script to do some grepping I have the following user file where the secondary groups are... (10 Replies)
Discussion started by: MaindotC
10 Replies

3. Shell Programming and Scripting

print all filenames in directory with path to another file

hi, i have a directory at /path/unix with the following files 1.txt 2.txt 3.txt 4.txt I want to make another file called filenames.txt at a different location called /path/home. So, my output file would be /path/home/filenames.txt with contents /path/unix/1.txt... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. UNIX for Dummies Questions & Answers

Sorting of alphanumeric filenames..

Hi All, I want sort following files in ascending order. exp_installer_CC4160-file10.dmp exp_installer_CC4160-file11.dmp exp_installer_CC4160-file12.dmp exp_installer_CC4160-file13.dmp... (3 Replies)
Discussion started by: gramakrishna
3 Replies

5. UNIX for Dummies Questions & Answers

Prepending information from filenames into files

I would like to know how to take information from a filename and place it into the text of the same file. Let's say I have a file called height_2_width_1.txt containing data that is related to a height of 2 and a width of 1, and the text originally looks like this: where these two columns... (13 Replies)
Discussion started by: Scatterbrain26
13 Replies

6. Shell Programming and Scripting

Truncate path and keep only filenames

Hi everyone, I have a question for you, I'm trying to create a script that will automate creating loading screens for the iPhone. So what I need to do, is list the directories inside /var/mobile/Applications and scan inside those, for the .app directory inside each. Take that .app name... (15 Replies)
Discussion started by: kicker75
15 Replies

7. Shell Programming and Scripting

Sorting by Full directory path

I have a text file with full list of files with their full path. I wanted to sort it by directory then files then subdirectory by alphabetically. When I used the sort command it doesn't give like what I want. Could somebody help me on this. Here is the ex: This is what I'm getting... (2 Replies)
Discussion started by: javidraaj
2 Replies

8. UNIX for Advanced & Expert Users

help with sorting sequence in Unix C:sort -t ':' +0 -1 -n +1 -2 +2 -3 -o list list

Hi List is 000|2008-07-17|556543|RTJ|35-RTGJ|EYT 465|2008-11-10|567789|GHJ|45-DGHH|ETU 533|2008-09-06|567789|GHJ|45-DGHH|ETU How does it do it? sort -t ':' +0 -1 -n +1 -2 +2 -3 -o list list (6 Replies)
Discussion started by: gurvinder
6 Replies

9. Shell Programming and Scripting

copying unreference files and keeping absolute path

Hi guys, I'm creating a script that basically remove unreference files so at the moment I have something like: DAYS=30 for DIRECTORY in `mount | awk '{ print $7}'` do find $DIRECTORY -type f -atime +$DAYS < ~/files.log done for FILE in `awk '{print $1}' ~/files.log` do cp... (2 Replies)
Discussion started by: hariza
2 Replies

10. UNIX for Advanced & Expert Users

Sorting filenames by order in another file

I would like to arrange /sort filenames ending with suffix like ".00XXXX". where X is a digit. However the order of arrangement is in a text file and is 'harpharzard'. e.g the text file may be like 002345 009807 001145 I wanted to avoid doing this using sql and exporting the text file back to... (4 Replies)
Discussion started by: samudimu
4 Replies
Login or Register to Ask a Question