![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Finding the oldest file in a particular directory | pavan_movva | Shell Programming and Scripting | 4 | 04-08-2009 07:24 AM |
| Finding the oldest file | janet | Shell Programming and Scripting | 5 | 03-23-2009 10:31 AM |
| Finding missing sequential file names | Julolidine | Shell Programming and Scripting | 2 | 07-02-2008 11:52 PM |
| Finding file version info | davewg | UNIX for Dummies Questions & Answers | 1 | 11-09-2007 01:24 PM |
| how to find capital letter names in a file without finding words at start of sentence | kev269 | Shell Programming and Scripting | 1 | 04-10-2006 09:35 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names
I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves.
Also, the script should only move a file from /exports to /staging if staging is empty; if there's already a file in /staging then the script would exit without doing anything. Here's an example of what the folders and files might look like: /exports job1_Friday_05302008.xml job1_Monday_05262008.xml job1_Monday_06022008.xml job1_Thursday_05292008.xml job1_Tuesday_06032008.xml job1_Wednesday_05282008.xml /staging In this case, because /staging is empty, the script should move /exports/job1_Monday_05262008.xml to /staging. The next time the script runs it should exit if /exports still contains job1_Monday_05262008.xml, otherwise it should move job1_Wednesday_05282008. Also, the "job1" prefix is static. I've got a script that uses ls and cat to loop through file names and parse out the date information but i'm getting bogged down with how to handle the results and i'm starting to think i might be going down the wrong path... Any suggestions would be appreciated |
|
||||
|
Look like a homework.
Please read Simple rules of the UNIX.COM forums: before posting, especially 5 and 6. |
|
||||
|
Clarification...
No, it's a real-world problem
It's just written out like a homework assignment because i've done enough technical work to know that when you ask for help, clarity is king. I'm working on an interface that gets daily xml exports from an ftp site and synchronizes them with Siebel CRM. I've got Oracle Fusion Middleware to: A) transfer files from the FTP site to an /exports folder C) route files from a /staging folder to an Siebel inbound web service. But I need a Step B) to meter out the appropriate files from /exports to /staging in the right order (first in, first out). I'm trying to do this with shell scripting but this is a bit outside my expertise, as you can tell... ![]() |
|
|||||
|
From my earlier post, you should be able to determine the oldest file. Are you there yet?
You can set a variable such like: Code:
old_file=$(cat exp_contents | tr "_" " " | awk '{print substr($3,5,4)"."substr($3,1,4)"|"$1"_"$2"_"$3}' | sort | head -1 | cut -d"|" -f2)
echo $old_file
|
|
||||
|
Working Script
Yes, it's working perfectly!
Here's the full script i'm using in case it's helpful for anyone else. It has an extra line at the top to purge files in an /archive folder that are > 7 days old. And the folder names and locations are changed a bit from what they were in my original post. But it seems to be working nicely. Thanks again for the help! Code:
#!/bin/ksh
find ../archive -name *.xml -type f -mtime +7 -exec rm {} \;
PROCESSING=$(ls ../processing | head -1)
if [[ -z $PROCESSING ]];then
INCOMING=$(ls ../incoming/*.xml | cat | tr "_" " " | awk '{print substr($3,5,4)"."substr ($3,1,4)"|"$1"_"$2"_"$3}' | sort | head -1 | cut -d"|" -f2)
if [[ -n $INCOMING ]];then
mv $INCOMING ../processing
fi
fi
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|