LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-09-2003, 05:50 PM   #1
wx_jason
LQ Newbie
 
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8

Rep: Reputation: 0
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!
 
Old 07-09-2003, 06:22 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Is your cron job a single command (like wget) or is it a shell script?
 
Old 07-09-2003, 06:32 PM   #3
wx_jason
LQ Newbie
 
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8

Original Poster
Rep: Reputation: 0
it is a shell scriptpulling from an ftp server
 
Old 07-09-2003, 06:43 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 07-09-2003, 07:11 PM   #5
wx_jason
LQ Newbie
 
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8

Original Poster
Rep: Reputation: 0
very good - thanks for the assist! i will give it a try!
and thios should work even though each file name is different?
 
Old 07-09-2003, 07:19 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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?
 
Old 07-09-2003, 07:47 PM   #7
wx_jason
LQ Newbie
 
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 07-09-2003, 09:17 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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?
 
Old 07-10-2003, 06:06 AM   #9
wx_jason
LQ Newbie
 
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 07-10-2003, 08:48 AM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 07-10-2003, 11:14 AM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux based gateway for time-based wireless service. TotalDefiance Linux - Software 0 10-03-2005 06:06 PM
Print a file if it exists and rename it at the same time. bhar0761 Linux - Enterprise 2 08-01-2005 11:51 PM
Print a file if it exists and rename it at the same time. bhar0761 Linux - General 3 08-01-2005 04:48 PM
grub doesn't auto-load kernel, time/date auto-change, k3b cannot load kenji1903 Red Hat 16 03-27-2005 08:48 PM
auto rename contents of a DIR bkeating Linux - General 1 11-05-2002 06:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration