Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2006-04-12
Medium Priority
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
  • 2
LVL 45

Accepted Solution

sunnycoder earned 1000 total points
ID: 16434673
Hi ch_kaushik,

ls -lrt | tail -1

if you wish to get only the name

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


Author Comment

ID: 16434753
Thanks Sunnycoder!


Author Comment

ID: 16435007
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 ...

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

571 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