There are multiple filenames in the directory.How to return the the lastest files for each file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting There are multiple filenames in the directory.How to return the the lastest files for each file name
# 1  
Old 02-18-2015
There are multiple filenames in the directory.How to return the the lastest files for each file name

there are mutiple file nams in the directory. How to return the the lastest files for each file name.

ex.

Code:
abc1234_050201
abc1234_050206
abc1234_050208
xyz34_050204
xyz34_050210
xyz34_050218

thanks

Last edited by Scrutinizer; 02-18-2015 at 04:10 PM.. Reason: code tags
# 2  
Old 02-18-2015
Try:
Code:
ls | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'

# 3  
Old 02-18-2015
the script still rturns lots file name but not the one for each name pattern. thanks
# 4  
Old 02-18-2015
This works for me:

Code:
✔ tmp1 $ ls
34567_a  34567_b  34567_c  34567_d  abc_a  abc_b  abc_c  abc_d  ascript.sh

+ tmp1 $ bash ascript.sh 
34567_d
abc_d

+ tmp1 $ cat ascript.sh 
newlist=""
for f in *_*;do
	nl=${f/_*/}
	echo "$newlist" | grep -q "$nl" || newlist+=" $nl"
done

for nl in $(echo $newlist|sort -u);do
	for this in $nl*;do echo > /dev/zero;done
	echo $this
done

hth
# 5  
Old 02-18-2015
With your sample file names I get:
Code:
ls | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'
xyz34_050218
abc1234_050208

What do you get?

Otherwise try:
Code:
ls xyz*_* abc*_* | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'
xyz34_050218
abc1234_050208

or
Code:
ls xyz*_* abc*_* | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'

Code:
\ls xyz*_* abc*_* | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add filenames to top of each files in a directory

Hello, I tried different solutions given in various linux portals but no luck.. The directory consists of files with no-extension. Each file has only one line. I need to add each filename to the top of each file so eventually each file will have two rows. Filenames have spaces between words... (6 Replies)
Discussion started by: baris35
6 Replies

2. UNIX for Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

3. UNIX for Dummies Questions & Answers

Is there any way to cat multiple files and show filenames?

Hi, Is there any way to do a cat * where it shows the name of each file in the process? Similar to what more does below? $ more ?.sql :::::::::::::: 1.sql :::::::::::::: set linesize 200 select db_unique_name, cast( from_tz( cast(... (5 Replies)
Discussion started by: newbie_01
5 Replies

4. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

5. Shell Programming and Scripting

Script to compare substrings of multiple filenames and move to different directory

Hi there, I am having trouble with a script I have written, which is designed to search through a directory for a header and payload file, retrieve a string from both filenames, compare this string and if it matches make a backup of the two files then move them to a different directory for... (1 Reply)
Discussion started by: alcurry
1 Replies

6. UNIX for Dummies Questions & Answers

Search filenames in directory, return filename only

Howdy, I am trying to search for a string within a directory, and then return the filename only, without the directory itself. I am using korn shell and the command x=$(find /XXXXX/XXXXX/XXXXX/I2 -exec grep -c "$line" {} /dev/null \;) which returns the directory and the filename: ... (4 Replies)
Discussion started by: jgrosecl
4 Replies

7. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

8. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. UNIX for Dummies Questions & Answers

How can I rename multiple files depending on a string occuring in the filenames?

I have many files that have "inputstring" somewhere in their filename (without the quotes), and I want to rename them all so that "inputstring" is replaced with "newstring". And I also want to specify arbitrary text for "inputstring" and "newstring" so that I can call the scripts that does this... (6 Replies)
Discussion started by: karman
6 Replies

10. Shell Programming and Scripting

Script to backup multiple files and amend their filenames

Hi, I'm trying to write a command that backs up certain files into my current directory and adds a prefix to the backed up file name. I realise this can be done in a script by specifying each individual file but would like to know if it can be done on one line and made an alias. I have the... (5 Replies)
Discussion started by: m223464
5 Replies
Login or Register to Ask a Question