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:
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!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+