auto file rename based on time
Hello!
Not real sure why I waited til just a few months ago to get into nix! very nice! at any rate i have be tinkering around with crond and download files at intervals throught the hour. Is there a way to rename the file automatically based on the most recent download? for example if file xx.xxxx comes in at 430 and there are already 20(xx.xxx1, xx.xxx2 and so on)or so files in the directory - i would like to rename the most recent to something like newest. Hope its clear - i have dug around a bit and come up blank. any suggestions would be awesome! Thanks! |
Is your cron job a single command (like wget) or is it a shell script?
|
it is a shell scriptpulling from an ftp server
|
Ok, try this:
1) Download the file 2) Rename the file using the date command 3) Create a symbolic link to the newly downloaded file I don't know how your script is set up, but I'm going to assume it knows the name of the file it's downloading, and for the purposes of the code below, I'm going to assume it's in a variable called FILENAME Code:
<snip: your initializations and ftp download command> Once that executes, you can always refer to the file newest_file to get the most up-to-date download. |
very good - thanks for the assist! i will give it a try!
and thios should work even though each file name is different? |
Yes. As long as the filename you download is in the FILENAME variable, then it will work.
If they are different filenames, you could probably get rid of all the date stuff. All that does is rename the file you downloaded to include the time (in hours and minutes). That's only necessary if you want multiple copies of the same named file; it would prevent the new download from writing on top of the older download. Make sense? |
i'm sorry - you lost me on that one - and i guess the wild card i used was incorrect?
here is a snip:" #!/bin/sh if [ -f /home/dir/dir/dummyfile ] then exit else touch /home/dir/dir/dummyfile cd /home/dr/dir ftp -n -p -i server quote user quote pass binary cd /remote dir newer sn.0000 newer sn.0001 etc etc through sn.0251 quit ! rm -f /home/dir/dirdummyfile current_time='date =%H_%M' mv "$sn.????" "{sn.????}_${current_time}" ln -s "{sn.????}_${current_time}" newest_file fi |
Ok, I know we can get this done, but I need some more clarification on what we're trying to do.
You've got a cron job that runs every so often. The cron job downloads files from an ftp server IF the one on the server is newer than your local copy So, when you download the file, does it overwrite the existing one? Say you've got a file sn.0001. The script runs, and there is a newer version of sn.0001 on the server. Is your local copy overwritten, or does ftp create a new file (something like sn.00001.1)? If it overwrites, then every file in the directory will be the "newest" one. So there would be no need to rename anything. Are you looking to know which file is the newest of the entire bunch? |
yes sorry for being so broad - i have the 251 files on my local and as you stated the newest is overwritten when a newer version is on the server. i have an external program that will parse this data based on a file name ("newest or current etc etc") so i can cron this program to generate a new data with the newest file. I suppose i could try to write another shell script and try to sync the two to update based on the original file names (sn.0000 etc etc) but this seems impractical and would be quite the long script as is the ftp. The server files are update every 6 to 10 minutes, my cron runs every 10. the external program would probably run every 12 or so to make sure the data has had time to be fully downloaded before being parsed.
As it stands now, I have to manually change the file name in the external program each time i want to see the new data - gets old pretty quick command lining every few minutes :) |
I'll give it some thought, but you may not need to use a script file per se. Are you familiar with the make command? It's typically used for compiling source code, but you can use it for anything you want. You create a Makefile, and you specify certain "rules". Everytime you run make, it will check to see if any dependencies are out of date. In this case, the dependencies will be the individual sn.XXXX files. If any of the are out of date, it will execute whatever commands you give it. I know that's pretty vague. I'll flesh it out some more when I have a little more time to post... meetings suck.
|
Ok, you might not need the newest file links stuff. Tell me if you think this will get what you need:
Code:
# record when the script started |
All times are GMT -5. The time now is 02:04 PM. |