- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Help with scripting
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 06:46 AM
тАО11-28-2005 06:46 AM
Help with scripting
I'd like some help in scripting the following.
A daily job that will analyze the files in a file system and then move off the files created the previous day to a heirarchical directory structure.
Where it gets a bit difficult is that they want to move it to a directory structure such as:
/Year/Month
/Year/Month/End Of Month
ex:
/Year/January
/Year/Januar/End Of Month
/Year/February
/Year/February/End of Month
So all files for the month, except the last day of the month get moved into the month directory. The files for the last day of the month get moved into the month/end of month directory.
The script will have to create the directories if they don't exist at the time the script is run. (first day of new month).
TIA and points for all responses.
Sean
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 07:03 AM
тАО11-28-2005 07:03 AM
Re: Help with scripting
MONTH=`date +%B`
mkdir /someotherdir/$MONTH
TODAY=`date +%b" "%d`
for FILE in `ll /somedir | grep -v "$TODAY" |awk '{print $9}`
do
mv $FILE /someotherdir/$MONTH
done
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 07:13 AM
тАО11-28-2005 07:13 AM
Re: Help with scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 07:58 AM
тАО11-28-2005 07:58 AM
Re: Help with scripting
It's easy to determine the first and last day of any month (including leap year) by simply comparing the current day to a predefined array of values (and adding one to February if the year is evenly divisible by four).
Given that, you can trigger a 'mkdir' to create any backup directory with the appropriate name.
Then, you might let your script 'touch' a "reference" file for the LAST day of the month thusly:
# touch -amt 11302359 /tmp/myref
Now, use 'find' to move everything but the last day to the month's directory:
# find /sourcedir -type f ! -newer /tmp/myref -exec mv {} /destdir \;
To move the last of the month's files to the end-of-month directory, simply drop the negatation ( the bang synbol, "!" ):
# find /sourcedir -type f -newer /tmp/myref -exec mv {} /destdir \;
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-28-2005 07:58 AM
тАО11-28-2005 07:58 AM
Re: Help with scripting
set -- `cal ` ; eval LDAY=\${$#}
echo $LDAY
CURMONTH=`date +%b`
Then, grep for "$CURMONTH $LDAY"
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-29-2005 08:16 AM
тАО11-29-2005 08:16 AM
Re: Help with scripting
No problem, couldn't be simpler.
Set up a cron job to run every day a few minuets after midnight.
Then based on the current date decide what your subdirectory is going to be and move the files with a modification data of greater than today
#!/usr/bin/ksh
date -u +%E" "%B" "%d | read YEAR MONTH DAY
LASTMONTH="you figure it out hint, case statment"
SUBDIR=${YEAR}/${MONTH}
# these work like if thens but simpler to read
# if the first test case succeeds the && makes shell run the next
# if the first test case fails it doesn't bother running
# the next test case sense the entire line has already failed
[ $MONTH = "Janruary" ] && [ $DAY = 1 ] && YEAR=$(expr ${YEAR} - 1)
[ $DAY = 1 ] && SUBDIR=${YEAR}/${LASTMONTH}/eom
[ ! -d $SUBDIR ] && mkdir -p $SUBDIR
find . -mtime +1 -exec mv {}";" $SUBDIR
I didn't test that at all so you may have to make a few tweeks here and there, or heck just rewrite it to suit your self. That├в s just the basic idea though. Moving the files is the easy part, deciding where to move them is more complicated.
H