Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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!


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  




Categorized: Shell Script Programming   (Article 6399, Written by )
Tagged: file system management, find, shell script programming
Previous: Why is my Mac hard drive suddenly read-only?
Next: Can't get standalone music player to work on MySpace?




Reader Comments To Date: 1

Paul Kosinski said, on April 9, 2006 2:24 PM:

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.

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, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.