LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   auto file rename based on time (https://www.linuxquestions.org/questions/linux-newbie-8/auto-file-rename-based-on-time-71392/)

wx_jason 07-09-2003 05:50 PM

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!

Dark_Helmet 07-09-2003 06:22 PM

Is your cron job a single command (like wget) or is it a shell script?

wx_jason 07-09-2003 06:32 PM

it is a shell scriptpulling from an ftp server

Dark_Helmet 07-09-2003 06:43 PM

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>

current_time=`date +%H_%M`
mv "$FILENAME" "${FILENAME}_${current_time}"

ln -s "${FILENAME}_${current_time}" newest_file

You can see what other options might suit you better by looking at man date.

Once that executes, you can always refer to the file newest_file to get the most up-to-date download.

wx_jason 07-09-2003 07:11 PM

very good - thanks for the assist! i will give it a try!
and thios should work even though each file name is different?

Dark_Helmet 07-09-2003 07:19 PM

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?

wx_jason 07-09-2003 07:47 PM

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

Dark_Helmet 07-09-2003 09:17 PM

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?

wx_jason 07-10-2003 06:06 AM

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 :)

Dark_Helmet 07-10-2003 08:48 AM

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.

Dark_Helmet 07-10-2003 11:14 AM

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
  start_time=`date +%H:%M`

  <the ftp stuff here>

  # record when the ftp downloads were complete
  stop_time=`date +%H:%M`

  # calculate how many hours passed between the start
  # of the script and the end of the downloads
  let hours_elapsed=`echo ${stop_time} | cut -f 1 -d ':'`-`echo ${start_time} | cut -f 1 -d ':'`

  # calculate how many minutes passed between the start
  # of the script and the end of the downloads
  let minutes_elapsed=`echo ${stop_time} | cut -f 2 -d ':'`-`echo ${start_time} | cut -f 2 -d ':'`

  # adjust the number of minutes elapsed by the number
  # of hours
  let minutes_elapsed=${hours_elapsed}*60+${minutes_elapsed}

  # get a list of files that have been modified in the past
  # X minutes with the find command
  file_list=`find /home/dir/dir -name "sn.????" -mmin -${minutes_elapsed}`

  for new_file in "${file_list}" ; do
    # instantiate your parsing script here
    do_something_to ${new_file}
  done



All times are GMT -5. The time now is 02:04 PM.