Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

List only latest file in directory

Hello Experts,

I have a directory with many files in it; it gets a new file written to it daily.  I want do a long list of only the latest file in the directory.  Can this be done?  I prefer to see only one file in my listing, the latest one by creation date.  I would settle for all the files created within the last day. I am running Solaris 2.6

Please reply with any ideas you may have.

Thanks,
believer
0
believer
Asked:
believer
  • 6
  • 3
  • 3
1 Solution
 
jonkeCommented:
How about this:

# ls -t |head -1
0
 
jonkeCommented:
sorry, long list - ls -lt |head -1
0
 
yuzhCommented:

if you only want the latest file, type in:

  # ls -alrt | tail -1

=====
yuzh
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jonkeCommented:
yuzh - thats a bit daft - as you use a -r argument to reverse the order, then tail instead of head ??!!
0
 
yuzhCommented:
look at the time stamp, we are typing the answer at the same time, the file is not locked when we are typing.
btw, your answer was not appear on my screen when i type it in.

====
yuzh
0
 
jonkeCommented:
fair enough!
0
 
yuzhCommented:
Be carefull, if you are using the command staight under you home dir, you might end up get:

.sh_history file as you out put,

to play safe use:

ls -alrt | grep -v .sh_history | tail -1
0
 
believerAuthor Commented:
Thanks for the replies, here is what I am seeing:

When I use: ls -alrt | tail -1 as yuzh suggested or ls -alrt | grep -v .sh_history | tail -1 as later suggested by yuzh, it gives me exactly what I want !

When I use : ls -t |head -1 as suggested by jonke I get  "total 6896"

Is this because my shell is KSH ?
0
 
believerAuthor Commented:
Thanks for your replies jonke and yuzh.
0
 
jonkeCommented:
The head -1 part tells it to run only output the first line of the ls -lt command which I didn't check first prints a total output on the first line. Points to Yuzh is fair enough I guess.

Just to help you believer - this is a pipe: "|". It takes ther output of one command and feeds into another command. So, "ls" is a listing. the arguments:

"a" - gives you hidden files (Those with a . in front)
"l" - gives you long listings
"t" - puts them in time stamp order
"r" - reverses that order

All this output is then piped into the head or tail commands with the agument -1 which I'm sure you can work out.

Check out man pages for these commands - man ls.
0
 
jonkeCommented:
The head -1 part tells it to run only output the first line of the ls -lt command which I didn't check first prints a total output on the first line. Points to Yuzh is fair enough I guess.

Just to help you believer - this is a pipe: "|". It takes ther output of one command and feeds into another command. So, "ls" is a listing. the arguments:

"a" - gives you hidden files (Those with a . in front)
"l" - gives you long listings
"t" - puts them in time stamp order
"r" - reverses that order

All this output is then piped into the head or tail commands with the agument -1 which I'm sure you can work out.

Check out man pages for these commands - man ls.
0
 
believerAuthor Commented:
jonke,

Thanks for the detailed response.  I am aware of the options, man pages and pipe.  I had not used head or tail commands before though.

Thanks also for being a good sport about the points award, the answer from YUZH was a turn-key solution for my question.

God Bless,
believer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now