Reading a field from the last line, after ls -lrt

Posted on 2006-04-12
Last Modified: 2010-08-05

can any one help me out with this one,

After typing in "ls -lrt", from the displayed contents, (i.e.)

Unix$>ls -lrt
-rw-r--r--   1 user1  group        2 Apr 12 15:06 123.dat
-rw-r--r--   1 user1  group        2 Apr 12 15:07 234.dat
-rw-r--r--   1 user1  group        2 Apr 12 15:08 345.dat
-rw-r--r--   1 user1  group        2 Apr 12 15:09 456.dat

I would like to read the latest updated file name, as here that would be "456.dat", using a shell script.

Thanks in advance.
Question by:ch_kaushik
    LVL 45

    Accepted Solution

    Hi ch_kaushik,

    ls -lrt | tail -1

    if you wish to get only the name

     ls -lrt | tail -1| awk ' { print $9 } '


    Author Comment

    Thanks Sunnycoder!


    Author Comment

    Sunnycoder one more point if you could reply,

    How to insert the current date format in between a filename?

    What I am trying is,

    Date=`date '+%Y%m%d'`
    File=`ls -lrt | tail -1 | awk '{print $9}'`
    mv $File $HOME/abc/NewFile_$Date_update.dat

    But it's failing ... any clues ...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This video teaches viewers about errors in exception handling.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    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

    12 Experts available now in Live!

    Get 1:1 Help Now