Script to cd into newest (dated) FTP directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to cd into newest (dated) FTP directory
# 1  
Old 01-11-2006
Script to cd into newest (dated) FTP directory

I have written the following simple bash script. It logs onto an FTP site and then CDs into a directory that corresponds to the last business day. So for example the directory I would access today is 20060110 (2006 jan 10).

I am currently taking today's date and subtracting 1, putting this into a variable and then entering ftp mode, cd'ing to the directory (in the variable) and downloading the file (always same filename). This obviously does not work for weekends and holidays, as subtracing 1 on monday would give sunday. There are no directories created on weekends and holidays.

My two ideas to rectify this are 1) do something complex to figure out the last business day before entering ftp mode or 2) figure out how to CD into the newest directory, regardless of its name (the newest directory will always be the previous day).

Any ideas are appreciated here. Maybe there is a much easier way to do this that I am not considering?

Thanks in advance for any help, this is my first post.

Steve D.
Chicago, Illinois

mydate=$(date +%F | tr -d [:punct:])
let prevdate=$mydate-1
user='xxxxxxx'
pass='xxxxx'

ftp -n ftp.xxxxxxx.com << ENDEND
passive
user xxxxx xxxxx
hash
cd $prevdate
get POA_133_Position.csv
quit
ENDEND
echo "Done."
exit 0
# 2  
Old 01-11-2006
you can use the datecalc function posted in the following link...

https://www.unix.com/unix-for-dummies-questions-and-answers/4870-days-elapsed-between-2-dates-post16559.html#post16559

if it is monday you can subtract -2 from current day so that you will get friday's date..

But for holidays, you can maintain a config file with holiday list, deduct -1 from current day and check whether this date is in config file, if it is there, then again deduct -1 from the previously arrived date ( current day - 1 ), again check in the config file... till it is not their in the config file. I don't think this will be too complex to do..
# 3  
Old 01-11-2006
Suppose this was Feb 1. You get 20060201. If you now subtract 1, you have 20060200, but there is no Feb 0. Go to our FAQ section and read the article on date arithemetic. Then switch to datecalc.
# 4  
Old 01-17-2006
According to me the best method to date calculations is taking epcoh time but for that you need a c program i dont know if there is any unix function is there
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Select the latest dated file-ksh script

Hello all!! I am new here and new in scripting! I want to write a ksh script to select the most recent file from a dir and use it in a variable. I have a directory with files named like: YYYMMDD A basic idea of the script I want to write is #!/usr/bin/ksh latest= latest_dated_file at... (2 Replies)
Discussion started by: chris_euop
2 Replies

2. Shell Programming and Scripting

Ftp script to output a directory name

Is there any way to include a directory name before the filename in the ftp session? Here is the script. DIRECTORY=`cat directory.txt|sed '/^$/d'` ( exec 4>&1 ftp -n>&4 2>&4|& print -p open $host print -p user $user $password print -p binary for D1 in... (0 Replies)
Discussion started by: ranjanp
0 Replies

3. Shell Programming and Scripting

Writing Script to Copy Newest Directory

I am trying to write a script that once executed it will search within a directory and copy only the newest directory that has not been copied before to a new location. Kind of like what ROBOCOPY /M does in windows? The directories are not left in the new location so using a sync action won't... (2 Replies)
Discussion started by: Keriderf
2 Replies

4. Shell Programming and Scripting

Script to check for the newest file mutiple times a day and SCP it to another server.

Hi, I need a sample of a script that will check a specific directory multiple times throughout the day, and scp the newest file to another server. Example: current file is misc_file.txt_02272011 (the last part is the date), once that has been secure copied, another one may come in later the... (1 Reply)
Discussion started by: richasmi
1 Replies

5. Shell Programming and Scripting

Mail file size of newest file in directory

I have been a long time lurker, and have learned a lot from these forums, thank you to everyone. I am using Zoneminder to record a security camera feed. No motion detection, just 24 hour recording. I then have a script that checks Mysql for events dated the day before, and throws them at... (4 Replies)
Discussion started by: iamVERYhungry
4 Replies

6. Shell Programming and Scripting

Get the newest file in a directory.

I am new to shell scripting so i need some help need how to go about with this problem. I have a directory which contains files in the following format. The files are in a diretory called /incoming/external/data AA_20100806.dat AA_20100807.dat AA_20100808.dat ... (4 Replies)
Discussion started by: ziggy25
4 Replies

7. UNIX for Dummies Questions & Answers

Create alias to daily dated directory

Hello, I wanted to see if there's a way to shortcut to a dated logs directory that changes daily...what I'm working with is something like this: > > > . . where the log directory is named yyyymmdd I've created a shortcut to get me to the /log directory but was... (2 Replies)
Discussion started by: mojowtm6
2 Replies

8. Shell Programming and Scripting

finding the 5 newest files in a directory

I have a folder that has new files created everyday, is there a way i can filter them so that only the 5 newest files are displayed? I'm hoping this can be done by a one liner without a script. for example my directory contains -rw-r--r-- 1 root root 0 Jun 24 08:34 file112 -rw-r--r-- 1 root... (2 Replies)
Discussion started by: zerofire123
2 Replies

9. Shell Programming and Scripting

Unix script to MOVE via FTP a directory

Good afternoon! I need to move a bunch of files from an FTP site to my server. However mget *, dele * will not suffice as I cannot guarantee that new files are not uploaded between commands. Roughly, this is what I would like to do: (using a .netrc) ftp somehost bin prompt cd... (1 Reply)
Discussion started by: abirdd
1 Replies

10. Shell Programming and Scripting

find full directory and delete old dated file?

I want to write a code that will look for a particular directory which is full or more than 95% used, and will search older file and delete it. My code looks like this: df -k|while read f1 f2 f3 f4 f5 f6 do if expr '$f5' -ge '95%' || continue then echo $f5 else echo "there is no... (2 Replies)
Discussion started by: xramm
2 Replies
Login or Register to Ask a Question