Solved

List only latest file in directory

Posted on 2002-04-17
12
254 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

860 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