Solved

List only latest file in directory

Posted on 2002-04-17
12
255 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
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!

 
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 100 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ovirt 3.6 guest VM support oracle solaris 4 86
aix tls version 6 532
AIX 5.x set up arrow to recall 11 63
SCP a file to multiple machines using a script 4 81
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.:
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.

735 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