i am having difficulties writing a script to back up files which have need modified during the month specified in the option. The command is:
-b username yyyymm
so if i say 200504. i will have to back up all the files in that specific month to my home directory
That’s an interesting problem, actually, and I’m sure it can be solved with the ever-useful find command, but there’s some fancy footwork involved in identifying files modified only during the given time segment.
A quick glance at the man page for the command (man find from a Terminal) reveals that the key flag we want to look at is -mtime since we want to match those files that have been modified in the specified time frame.
I’m going to make an assumption, though, because it’ll make life a lot easier: I’m going to assume that you have the far more powerful GNU find command, which offers the -newer flag which has lots of modifiers, most notably that it allows more sophisticated time and date specifiers.
The -newer flag also allows you to specify that you’re interested in access time, change time, or modification time (a, c, and m, respectively), both for the file you’re testing and any other file or, if you use the “t” modifier, a date specification.
That’s not very clear, so let me show you how this works for our application: -newerct will let us specify a date that should be compared to the change time of each file examined, exactly what we want.
Here’s a useful example from the find man page:
This lets you easily identify files that have been changed within the last minute.
So that’ll do what we want, and you can easily transform a YYYYMM form into something that find will understand by slipping a dash in the middle: YYYY-MM. This can be done within a script with the following:
Here I am assuming that the variable “arg” already has the YYYYMM field loaded.
Now the hard part: how do you isolate just that month’s changes, and not end up getting everything modified between the beginning of that month and today. That’s not a feature that find offers (you can’t do -older, for example and use a structure like “find -newer 10 -older 15”).
Here’s my thinking, and, yeah, it’s a bit of a complex solution: run the find command to pull out a list of all files modified since the beginning of the specified month, then increment the month one and run the find again. Now just diff the two output files and anything that appears in the first, larger output file, but not the second is what you need to back up to complete this task.
I hope that makes sense. If someone else has a different idea for accomplishing this task, let’s hear about it!