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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-09-2003, 05:50 PM
|
#1
|
LQ Newbie
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8
Rep:
|
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!
|
|
|
07-09-2003, 06:22 PM
|
#2
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
Is your cron job a single command (like wget) or is it a shell script?
|
|
|
07-09-2003, 06:32 PM
|
#3
|
LQ Newbie
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8
Original Poster
Rep:
|
it is a shell scriptpulling from an ftp server
|
|
|
07-09-2003, 06:43 PM
|
#4
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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.
|
|
|
07-09-2003, 07:11 PM
|
#5
|
LQ Newbie
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8
Original Poster
Rep:
|
very good - thanks for the assist! i will give it a try!
and thios should work even though each file name is different?
|
|
|
07-09-2003, 07:19 PM
|
#6
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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?
|
|
|
07-09-2003, 07:47 PM
|
#7
|
LQ Newbie
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8
Original Poster
Rep:
|
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
|
|
|
07-09-2003, 09:17 PM
|
#8
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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?
|
|
|
07-10-2003, 06:06 AM
|
#9
|
LQ Newbie
Registered: Jul 2003
Distribution: RH 8.0
Posts: 8
Original Poster
Rep:
|
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
|
|
|
07-10-2003, 08:48 AM
|
#10
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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.
|
|
|
07-10-2003, 11:14 AM
|
#11
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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 06:36 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|