Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2006
    Posts
    3

    Wink Unanswered: I need your help.

    Hi
    I have a multiple files in the same directory . The only different between these file is the date stamp.
    For example : a_20060308.dat
    a_20060309.dat

    without the datestamp, these files will have the same file name (which is a)
    and of course, these two have different created date.

    I just want to pickup the lastest file and mv that latest file to different location. Does anyone knows or have any idea how to get this using korn shell or awk/sed ...? Thank you so much in advance.

  2. #2
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    If you want to do it by timestamp you can write:
    mv $(ls -ltr a_*.dat | awk 'END{print $9}') somelocation
    where the '$9' in the awk statement represents the ninth (last) field in the ls -l outputline. This is the filename on many UNIX / Linux systems. By using ls -ltr the listing is sorted on t(ime)r(eversely).

    Regards

    BTW. This is Linux syntax, if it doesn't work on your system you always can use `` instead of $().

  3. #3
    Join Date
    Sep 2006
    Posts
    3

    Re: I need Your help

    TYVELEYN

    Thank you so much for your help regarding this matter. I'll try this approach and will let you updated. Again, Thank you and Have a nice weekends.

    Best Regards,
    LEO111.

  4. #4
    Join Date
    Sep 2006
    Posts
    3
    TYVELLEYN
    The files name is only example... The problem is I have lot of file name and I don't want to specify in my scripts what is the name of the files. Whatever files are there , I just want to pick up the lastest one if duplicate. So I can not hard coded the file name there. Is there a way to get rid of the datestamp first then apply the ls -ltr command to it to get the lastest file. Thank you.

  5. #5
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    e.g.
    latestfile=$(ls -A1t /tmp |head -1)

    What do you mean by "if duplicate"
    There can't be duplicate filenames in a directory
    Do you want to sort on file name?
    Just use ls -A1r instead

  6. #6
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    Maybe this is what you're looking for:
    Code:
    #!/usr/bin/ksh                                                                
                                                                                  
    for i in $(ls -tr *[12][09][0-9][0-9][01][0-9][0-3][0-9]*); do
        cp $i somelocation/$(echo $i | sed 's/^\(.*[^0-9]\)[12][09][0-9][0-9][01][0-9][0-3][0-9]\([^0-9].*\)$/\1\2/')
    done
    This is a very basic algorithm for detecting files with a datevalue in their names (yyyymmdd). It cuts out the datevalue from every filename found and writes the original files to a file with the stripped name.
    E.g. every a_YYYYMMDD.dat file will be written to 'a_.dat' and every b_YYYYMMDD.dat file to 'b_.dat'. Because the filenames are fetched with 'ls -tr' (adopted from pdreyer, didn't know it would work without the long list...) the files are reversely sorted by timestamp and thus overwrite the previous (= older) file with the target filename.

    Regards
    Last edited by Tyveleyn; 09-18-06 at 05:37.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •