Sponsored Content
Top Forums Shell Programming and Scripting backup files for a specific month Post 302072092 by thestevew on Thursday 27th of April 2006 10:33:02 AM
Old 04-27-2006
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
~

any help?

cheers

Last edited by thestevew; 04-28-2006 at 07:26 AM..
 

10 More Discussions You Might Find Interesting

1. HP-UX

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies

2. Solaris

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies

3. UNIX for Advanced & Expert Users

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies

4. Shell Programming and Scripting

backup of files for a specific date

I want to write script for backing up archive logs files for specific date. please give me idea for that. (2 Replies)
Discussion started by: akm9999
2 Replies

5. Filesystems, Disks and Memory

backup of files for a specific date

In Linux Advance server I want to write one script for backing up files for a specific date like 24/07/2008. (3 Replies)
Discussion started by: akm9999
3 Replies

6. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

7. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

8. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

9. Shell Programming and Scripting

Three month old specific files deletion

Hi, I need to delete 3 month old files in my logpath. This path contains several logs and other important files The file names are be like this sl_details.env tomcatfiles_03062014.log application_zur_03.062014.log I need to delete only tomcatfiles logs. I wrote this command. can... (7 Replies)
Discussion started by: nag_sathi
7 Replies

10. UNIX for Advanced & Expert Users

Get last month files

Hi All, How to get last month files. Ex : 1st Jan i have to get Dec 31 days files and on Feb 1st i have to get Jan 31 days files and on Mar 1st i have to get Feb 28 days files. Below are the example files with date and timestamp. aaa.txt.timestamp aaa.txt.timestamp aaa.txt.timestamp Please... (7 Replies)
Discussion started by: kiranparsha
7 Replies
CAL(1)							    BSD General Commands Manual 						    CAL(1)

NAME
cal -- displays a calendar SYNOPSIS
cal [-smjy13] [[[day] month] year] DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the current month is displayed. The options are as follows: -1 Display single month output. (This is the default.) -3 Display prev/current/next month output. -s Display Sunday as the first day of the week. -m Display Monday as the first day of the week. -j Display Julian dates (days one-based, numbered from January 1). -y Display a calendar for the current year. -V Display version information and exit. A single parameter specifies the year (1 - 9999) to be displayed; note the year must be fully specified: ``cal 89'' will not display a calen- dar for 1989. Two parameters denote the month (1 - 12) and year. Three parameters denote the day (1-31), month and year, and the day will be highlighted if the calendar is displayed on a terminal. If no parameters are specified, the current month's calendar is displayed. A year starts on Jan 1. The first day of the week is determined by the locale. The Gregorian Reformation is assumed to have occurred in 1752 on the 3rd of September. By this time, most countries had recognized the ref- ormation (although a few did not recognize it until the early 1900's.) Ten days following that date were eliminated by the reformation, so the calendar for that month is a bit unusual. HISTORY
A cal command appeared in Version 6 AT&T UNIX. OTHER VERSIONS
Several much more elaborate versions of this program exist, with support for colors, holidays, birthdays, reminders and appointments, etc. For example, try the cal from http://home.sprynet.com/~cbagwell/projects.html or GNU gcal. AVAILABILITY
The cal command is part of the util-linux-ng package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/. BSD
June 6, 1993 BSD
All times are GMT -4. The time now is 04:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy