Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

List only latest file in directory

Posted on 2002-04-17
12
Medium Priority
?
258 Views
Last Modified: 2013-12-27
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
Comment
Question by:believer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 2

Expert Comment

by:jonke
ID: 6947925
How about this:

# ls -t |head -1
0
 
LVL 2

Expert Comment

by:jonke
ID: 6947927
sorry, long list - ls -lt |head -1
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6947938

if you only want the latest file, type in:

  # ls -alrt | tail -1

=====
yuzh
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:jonke
ID: 6948020
yuzh - thats a bit daft - as you use a -r argument to reverse the order, then tail instead of head ??!!
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6948034
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
 
LVL 2

Expert Comment

by:jonke
ID: 6948047
fair enough!
0
 
LVL 38

Accepted Solution

by:
yuzh earned 400 total points
ID: 6948048
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
 
LVL 1

Author Comment

by:believer
ID: 6948092
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
 
LVL 1

Author Comment

by:believer
ID: 6948766
Thanks for your replies jonke and yuzh.
0
 
LVL 2

Expert Comment

by:jonke
ID: 6950009
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
 
LVL 2

Expert Comment

by:jonke
ID: 6950043
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
 
LVL 1

Author Comment

by:believer
ID: 6950585
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

721 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