1854791 Members
4662 Online
104103 Solutions
New Discussion

Question about "ls -ltr"

 
SOLVED
Go to solution
Hunki
Super Advisor

Question about "ls -ltr"


I have the following out from the "ls -ltr" output :


prot@atstb:/appl/tst/test/smt> ls -ltr |grep "prot.*"
drwxr-xr-x 3 atm tst 512 Sep 12 2005 prot.0221
drwxr-xr-x 3 atm tst 512 Dec 28 2005 prot.dec05
drwxr-xr-x 3 atm tst 512 Feb 21 2006 prot.0607
drwxr-xr-x 3 atm tst 512 Jun 7 2006 prot.0410
drwxr-xr-x 4 atm tst 512 Apr 12 16:25 prot.0412
drwxr-xr-x 4 atm tst 512 Apr 13 12:21 prot.0413
drwxr-xr-x 3 atm tst 512 Apr 13 12:23 prot.0416
drwxr-xr-x 4 atm tst 512 Apr 17 15:53 prot.0417
drwxr-xr-x 4 atm tst 512 May 10 12:43 prot.0510
drwxr-xr-x 3 atm tst 512 May 10 12:45 prot

Requirement :
I need to get the penultimate value of the listing ( in this case prot.0510) , which can vary each time I do an install of the prot module and out of that I need to save 0510 in a variable. How do I achieve that . Please help.
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor

Re: Question about "ls -ltr"

Hi Hunki:

If you want the most recent first, drop the '-r' switch:

# ls -lt

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: Question about "ls -ltr"

This may work:

VAR=$(ls -ltr prot.* | awk -F . '{print $2)')

Try the statement on the command line before assigning to a variable.

# ls -ltr prot.* | awk -F . '{print $2)'
Patrick Wallek
Honored Contributor

Re: Question about "ls -ltr"

Actually, my first post won't do what you want. You need to add a 'tail -1' to it. Try this:

# ls -ltr prot.* | tail -1 | awk -F . '{print $2}'

To assign to a variable:

VAR=$(ls -ltr prot.* | tail -1 | awk -F . '{print $2}')
spex
Honored Contributor
Solution

Re: Question about "ls -ltr"

Hi Hunki,

If you vary the input much, this will break:

$ myvar=$(ls -1rt | tail -n 2 | head -n 1 | cut -d. -f2)

PCS
James R. Ferguson
Acclaimed Contributor

Re: Question about "ls -ltr"

Hi (again):

OK, try this:

# ls -lt /path|awk '{if (NR==2) {split($NF,a,".");print a[2]}}'

This skips the line beginning with "total" and extracts the most recent file's name where the suffix follows a "." character.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Question about "ls -ltr"

Hunki:

Asking for help and assigned zero points to an attempt is not conducive to getting help in the future.

...JRF...
Hunki
Super Advisor

Re: Question about "ls -ltr"

Can I be able to reassign points to each one of you.

thanks,
hunki
Torsten.
Acclaimed Contributor

Re: Question about "ls -ltr"

unfortunately not - give more points next time ...

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Patrick Wallek
Honored Contributor

Re: Question about "ls -ltr"

So what was wrong with the solutions? I am almost 99% certain that mine will work.
James R. Ferguson
Acclaimed Contributor

Re: Question about "ls -ltr"

Hunki:

...and did you actually try mine?

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: Question about "ls -ltr"

Hi,
VAR=$(ls -ltr prot.*|awk -F . 'END {print $2}')

HTH,
Art
Peter Nikitka
Honored Contributor

Re: Question about "ls -ltr"

Hi,

since the prot.* arguments are directories:

ls -dt prot.* | awk -F. '{print $NF;exit}' | read myval

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"