Computing.Net > Forums > Unix > Sort filename pick latest and delete rest

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sort filename pick latest and delete rest

Reply to Message Icon

Name: Kaveri
Date: May 11, 2009 at 06:26:30 Pacific
OS: Windows XP
Subcategory: General
Comment:

Hi All,
I have a requirement something like this..there are set of files with name sample_plant_date..I need to sort in acesnding order based on plant and date filed and then retain only the latest file in the directory for a particular while delete the rest.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 11, 2009 at 07:12:41 Pacific
Reply:

First, I am assuming you want a Unix script:

#!/bin/ksh

# cd <to your directory>

# skip the first filename, remove the rest
fread=0
ls -1t sample_plant*|while read fname
do
   if [[ fread -eq 0 ]]
   then
      fread=1
      continue
   fi
   rm "$fname"
done


0

Response Number 2
Name: Kaveri
Date: May 11, 2009 at 23:00:26 Pacific
Reply:

Thankyou .But the problem here is , I have thousands of file for different plants and timestamp, I need to retain the latest file for all the plant code.
sample_1006_045600.txt
sample_1009_045500.txt
sample_1011_045400.txt
sample_1006_052500.txt
sample_1011_025600.txt

ie.. my resultant files should be
sample_1009_045500.txt
sample_1011_045400.txt
sample_1006_052500.txt

Again here there are only few files but in real scenario , I will be having thousands of files for different plants.
Regards,
Kaveri


0

Response Number 3
Name: nails
Date: May 12, 2009 at 07:13:02 Pacific
Reply:

What you are asking for is rather difficult. Can I assume that the plant id is always columns 8 to 11?

sample_1009_045500.txt


0

Response Number 4
Name: lankrypt0
Date: May 12, 2009 at 08:30:04 Pacific
Reply:

to make sure i understand, you only need the latest file for EACH plant, not the latest file in the directory?


0

Response Number 5
Name: lankrypt0
Date: May 12, 2009 at 08:59:00 Pacific
Reply:

#!/usr/bin/ksh
for var in $(ls *.txt | sort -t_ -k1,2|awk -F_ '{print $1"_"$2}'|sort -u);do
keepfile=$(ls|grep $var|tail -1)
cp $keepfile $keepfile.keep
done
rm *.txt
for var in $(ls *.keep);do
mv $var $(echo $var|awk -F. '{print $1"."$2}')
done

The above will delete the non-oldest files immediately, I am not sure what you meant by "retain only the latest file in the directory for a particular while".

Since this is deleting files, when you test, make sure you create a test directory, dont use live data.


0

Related Posts

See More



Response Number 6
Name: Kaveri
Date: May 12, 2009 at 21:45:56 Pacific
Reply:

Excellent code .Thankyou :).Please me know if I can rate you somewhere as I am new to UNIX forum.


0

Response Number 7
Name: lankrypt0
Date: May 13, 2009 at 04:38:03 Pacific
Reply:

glad it worked out for you. Not sure about any ratings things here, actually.


0

Response Number 8
Name: ajimon
Date: May 29, 2009 at 10:45:03 Pacific
Reply:

excellent...............


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Sort filename pick latest and delete rest

Removing Duplicates www.computing.net/answers/unix/removing-duplicates/2495.html

Merging two data files www.computing.net/answers/unix/merging-two-data-files/7293.html

filename problems www.computing.net/answers/unix/filename-problems/4934.html