Solved

List only latest file in directory

Posted on 2002-04-17
12
252 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
Comment Utility
How about this:

# ls -t |head -1
0
 
LVL 2

Expert Comment

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

Expert Comment

by:yuzh
Comment Utility

if you only want the latest file, type in:

  # ls -alrt | tail -1

=====
yuzh
0
 
LVL 2

Expert Comment

by:jonke
Comment Utility
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
Comment Utility
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
Comment Utility
fair enough!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 38

Accepted Solution

by:
yuzh earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your replies jonke and yuzh.
0
 
LVL 2

Expert Comment

by:jonke
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now