Free tech support / small logo


How do I identify files changed during a specific month?

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


Dave's Answer:

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:

find . -newerct '1 minute ago' -print

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:

newdate="$(echo $arg | cut -c1-4)-$(echo $arg | cut -c5-6)"

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!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

Have the find command run a custom time-filtering script via the '-exec' option. Something like:

      find . -newerct '1 minute ago' -exec /home/user/custom-filter \{\} \;

That script can use 'stat' to examine the timestamp and then output the names of the files that pass via 'ls' (or 'ls -l').

Note: It's important to escape the "{};" with backslahes, and to separate the "\;" from the "\{\}" with a space.

Posted by: Paul Kosinski at April 9, 2006 2:24 PM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.