1753797 Members
6990 Online
108799 Solutions
New Discussion юеВ

Re: Print last filename

 
syedar
Advisor

Print last filename

Hi all,

I need to print last file name when executing "ls -ltr" command from a fixed path.

Example:
$ls -ltr
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:08 abc123
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:10 abc124
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:15 abc125
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:20 abc126
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:25 abc126
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:30 abc127
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:40 abc128
-rw-rw-rw- 1 RT R1 6922674 Jul 8 14:50 abc129
-rw-rw-rw- 1 RT R1 6922674 Jul 8 15:08 abc130

8 REPLIES 8
likid0
Honored Contributor

Re: Print last filename

do you mean ?

#ls -ltr | tail -1
Windows?, no thanks
James R. Ferguson
Acclaimed Contributor

Re: Print last filename

Hi:

It's not clear exactly what you mean, so:

# ls -ltr | awk 'END{print $NR}'
abc130

# ls -ltr | tail -1
-rw-rw-rw- 1 RT R1 6922674 Jul 8 15:08 abc130

# ls -ltr | awk '{print $NF}'
abc123
abc124
abc125
abc126
abc126
abc127
abc128
abc129
abc130

Regards!

...JRF...
syedar
Advisor

Re: Print last filename

i just need to print "abc130".
James R. Ferguson
Acclaimed Contributor

Re: Print last filename

Hi (again):

> i just need to print "abc130".

# ls -ltr | awk 'END{print $NR}'
abc130

...as I said...

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Print last filename

>I just need to print "abc130".

Or leave out the long listing and print first:
$ ls -t | head -1
syedar
Advisor

Re: Print last filename

Thanks JRF.
James R. Ferguson
Acclaimed Contributor

Re: Print last filename

Hi Syedar:

If you are satistied with the answers you have received, please read:

http://forums.itrc.hp.com/service/forums/helptips.do?#28

Assigning points is both a way of saying "thank you" and a way of showing future readers of your thread what helped you solve your problem.

Most of your threads have no points assigned. You can review those, too, by examining your own profile:

http://forums.itrc.hp.com/service/forums/publicProfile.do?userId=WW217887&forumId=1

Regards!

...JRF...
Nitin Kumar Gupta
Trusted Contributor

Re: Print last filename

ls -1tr | tail -1

-NKG-