
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!
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
Shell Script Programming
(Article 6399)
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? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 PMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|