LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   How to get last file written to directory (https://www.linuxquestions.org/questions/linux-networking-3/how-to-get-last-file-written-to-directory-163261/)

kd5giv 03-28-2004 12:34 AM

How to get last file written to directory
 
Hello,
Suppose I want the last file written to an ftp directory. Does anybody know how to write a script to get the latest file from an ftp directory.

There is 250 or so files in that directory. It's weather radar data files for some software I use to plot it. Since the National Weather Service didn't make file names that are named as dates or make a symbolic link called latest, I can't get the files by file name. So I need to get the latest file by when it was put in the directory.
If I don't have the right ftp client I can get it, but I usually use wget or ncftp that came with Slackware 9.1.

Thanks for listening to my long story.

Oliv' 03-28-2004 06:45 AM

Hello,

to sort file by date, you can use: ls -t ;)
So what you can do is a script which connect you with ssh, then list file by date and grep the good ones... you put their address in a file and thanks to wget -i, you can retrieve your files :)

oliv'

kd5giv 03-28-2004 06:36 PM

Well what you said helped some. I now have the ls -lt. Now I am having problems with grep. I've never run grep. Maybe I can get some help with grep now. Here is the listing I need to be put into a file:

-rw-r--r-- 1 1 18406 Mar 28 22:07 sn.0232

I need the sn.0232 at the end of the list there into a file for wget to use. I thought of using 'cut' but it cut the last few letters of the whole directory. That output above is from the second line. of ls -tl. Using cut I did 'cut -b 52-63' or something like that.
Thanks oliv' for your help though.

Oliv' 03-29-2004 06:46 AM

Hi,

So I suppose that you need to download file of the current day... 'cause when you run "grep" command you need to match a pattern and as you may not know filename by advance, we'll do work on the date ;)
So to have the date in the same format as "ls -l" command, the command is "date +%b\ %d" (the \ is required to protect space character)... So the command to list the files created the current day is:
Code:

ls -lt | grep "$(date +%b\ %d)"
then you can run "cut" command to only keep filename and redirect the result into your file :)

Oliv'

kd5giv 03-29-2004 02:26 PM

Thanks for the help on this subject. I found a short cut for this problem. the command:
ncftpls -xt ftp://tgftp.nws.noaa.gov/SL.us008001...p19r0/SI.kfws/ | head -1 > outfile
gets the latest file name output to the file named 'output'
This post is in case somebody is looking for the same problem later and does a search for questions already asked.


All times are GMT -5. The time now is 03:10 AM.