UNIX scripting - ksh - get part of filename as valiable

Posted on 2011-05-05
Last Modified: 2013-12-26

Would like to see somebody's help on writing small ksh script runs on UNIX.

Environment : Solaris 8/10
in one directory say /var/tmp , there are multiple files in below manner.
i.e. abc_20110501 , abc_20110502, abc_20110503

I would like to get "latest file's filename , only date part, for this case,   only for "20110503"
within ksh script.

Would somebody kindly help?
Question by:sleepingluke
    LVL 48

    Expert Comment

    cd /var/tmp
    file=$(ls abc*)
    echo ${file#*_}

    Open in new window

    LVL 48

    Expert Comment

    Small correction

    file=$(ls abc*)

    Open in new window

    should be

    file=$(ls -t abc*)

    Open in new window

    assuming you're basing the latest file on modification date.
    LVL 6

    Accepted Solution

    Tintin's soln assumes that the timestamps on the file are matching the timestamps in filename. Without that it won't work. E.g. if you transfer the files to a different place.

    Use this if you really want to sort by the timestamp given in the file name:
    // assuming YYYYMMDD format:
    LATEST_DATE=`ls -1 /var/tmp/abc_* | sed 's/abc_//' | sort | tail -1`
    // or you can use cut as well if the prefix is fixed length (always abc_)
    LATEST_DATE=`ls -1 /var/tmp/abc_* | cut -b 5- | sort | tail -1`

    Open in new window

    LVL 37

    Assisted Solution

    by:Gerwin Jansen
    LATEST_DATE=`ls -1tr abc_* | tail -1 | cut -d"_" -f2`


    ls -1tr abc_*
    # list all abc_* files in 1 column, sorted by date, most recent file last

    tail -1
    # get only last file = most recent file

    cut -d"_" -f2
    # cut string at _ and print 2nd part (date

    Author Closing Comment

    thanks all for helpful tips and knowledges!
    I could workout with theKashyap second command as date should be fixed length for a time being.

    LVL 11

    Expert Comment

    Note that you don't need ls's "-1" switch in this context (i.e. just "ls" will give the same result here).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Video by: Phil
    This video goes over how to configure and start a jail in FreeBSD.  This video is meant to supplement the article included with this course.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now