![]() |
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 |
| find files modified in a specific month | omer_ome | UNIX for Advanced & Expert Users | 1 | 07-02-2006 09:43 AM |
| find files modified in a specific month | omer_ome | SUN Solaris | 1 | 07-02-2006 09:38 AM |
| find files modified in a specific month | omer_ome | HP-UX | 1 | 07-02-2006 09:36 AM |
| How to get previous month files | savitha | Shell Programming and Scripting | 7 | 03-23-2006 05:16 AM |
| Find all files by month | maldini | Shell Programming and Scripting | 13 | 08-03-2005 01:22 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
backup files for a specific month
i am having a problem with writing a shell script to back up files for a specific month. the month and year are specified as paramters. so a backup of all the files modified during the specified month have to be made. for example if specify 200406 as my parameter, it should back up files that have been modified during the month of june in year 2004
i know u can use the find -atime -n. but it aonly finds files that were accesed within n days. but i would like to find files for a specific month and back it up. need help please thanks |
|
||||
|
I think u will need to find the diff of current that with the start and end date of the month u want backup.
and u may use the find . -mtime -60 -mtime +30 -mtime -60,...less than 60 days ago -mtime +30...more than 30 days ago.. this way u will have to look for yours month duration... but there should be better ways...may be someone else in group can help |
|
||||
|
An alternative is to try to match the Month and year in a directory listing - tho the change from "dd Mmm hh:mm" format to "dd Mmm yyyy" when a file gets to six months old complicates this.
I would be a good idea to log the details of files deleted too! Code:
Code
# note proper parameter validation (e.g. mon between 1 and 12 inclusive)
# script also assumes that you have read/write access to all subdirectories
scriptname=$(basename $0)
if [ $# -ne 3 ]; then
print "usage: $scriptname MonthNumber YearNumber directory, e.g. '$scriptname 4 2006 /tmp'"
exit 0
fi
set -A monnames Zero Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
typeset -i mon=$1
typeset -i yr=$2
typeset -i thisyr=$(date +%Y)
typeset -i lastyr=$(($thisyr -1))
typeset -i thismon=$(date +%m)
typeset -i fmon
print "Print Deleting files from ${monnames[$mon]} $yr in directory $3"
find $3 -type f -ls | sed -e's/ *//' | tr -s ' ' | cut -d' ' -f8- | while read fmonname fday fyrtime ffilename
do
# Get month number from month name in file list
fmon=1
while :
do
if [ $fmonname = ${monnames[$fmon]} ]; then
break
fi
fmon=$(($fmon + 1))
done
# if the last part of the timestamp is a time rather than a yr then
# we need to find the yr it belongs to
if [[ $fyrtime = *:* ]]; then
# if month number is not greater than current month number then it is in this yr ...
if [ $fmon -le $thismon ]; then
fyrtime=$thisyr
else
# ... otherwise it's last yr
fyrtime=$lastyr
fi
fi
if [ $fmon = $mon ]&&[ $fyrtime = $yr ]; then
print "deleting $fname dated $fday $fmon $fyrtime $ffilename"
-- backup $ffilename somehow --
fi
done
~
cheers Last edited by thestevew; 04-28-2006 at 06:26 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|