Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO FIND THE LATEST FILE BY FILE'S EXTENSION 1

Status
Not open for further replies.

vaanand

Programmer
Apr 5, 2001
2
US
I am a UNIX newbie so excue me for my simple question. I am using HP-UNIX. The problem is I want to log into a remote server and pick out one file whose file extesion is the latest date and FTP it back into my local server . And I also want to get the name of the file I picked so that I have to use this file name for some other purpose. The whole thing should be korn shell script so that I can automate these processes.For example in the remote server there could be 4 or 5 files with same name but with different date extension as zzz.20010405,zzz.200010404,zzz.20010403. I need pick out the highest dated file extension. My script should pick zzz.20010405 and FTP this file to my local server and I need to get this file name into variable for other processing.

2) problem 2 is after FTP ing the largest date extension file to my local server i need to move these file in the remote server to another archive directory in the same remote server from Home directory of the remote server.

Thanks in advance.
Andy
 
Hi, vaanand!

Please, try this command for the first problem:

  ls -1 | awk '$1 > prev { prev = $1 }  END { print prev }'

Command ls -1 lists one file per line.

$1 in awk script can be $0.  The highest dated file has name that is last in alphabetic order - awk script will pick up his name. Awk is suitable for this problem.

This command works if in directory are only zzz.* files.

Bye!

KP.
 

Hi again, vaanand!

Another way to do this job:

ls -1 | sort -r | awk 'NR == 1'

Command sort -r sorts the output of ls -1 in reverse order. Awk command prints first line of input.

Bye!

KP.
 

Again me, vaanand! Hi!

Now I have a better idea:

ls zzz.* -1 -r | awk 'NR == 1'

Command ls zzz.* -1 -r lists zzz filenames in reverse order.

I think, that's all from me to you for today.

Bye!

KP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top