• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [OT] shell scripting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OT] shell scripting


  • Subject: Re: [OT] shell scripting
  • From: "Adam K. Wuellner" <email@hidden>
  • Date: Thu, 4 Nov 2004 16:42:23 -0600

'Somewhat OT'?  heh...

On Nov 4, 2004, at 1:03 PM, John C. Welch wrote:
[...]
The theory is pretty simple...if it's older than x days, do blah.
[...]
I'm going to run this as a daily cron, so I'm not going to use applescript.
However, I'm not a shell expert by any means, so if anyone has some advice
on how to do this, I'd appreciate it.

Well, I'm not sure if what I've got will be much help, but I've been toying with a similar idea for managing my Safari downloads folder. Basically, I wanted to roll the folder into an archive every so often...


1. finding files last modified more than 60 days ago

find /path/to/start/at -not -newermt '60 days ago'

-newerXY
long and short of it: if X=m and Y=t, you will find files whose modification time is newer than the time argument that follows, which is in the form recognized by cvs(1). There is no -older expression, you have to use -not -newer.
see `man 1 find`


2. getting useable posix paths

find(1) returns unquoted paths to the files it finds. It has an option, -X, designed to make the output xargs(1)-friendly, but it accomplishes this by simply omitting any files with unfriendly characters from its output. :-O
I've seen scripts that overcome this by piping the find(1) output to sed(1) for replacement of spaces with escaped spaces, for example. This might be sufficient, or you might need to escape other characters, too.


find /path/to/start/at -not -newermt '60 days ago' | sed 's/ /\ /g'

3. acting on each of the files found

#!/bin/bash
find /path/to/start/at -not -newermt '60 days ago' | sed 's/ /\ /g' | while read filename
do
# do something with the file name, which is now stored in $filename
done


[watch for wrapping; there are five lines above, starting with '#', 'find', 'do', '#', and 'done'.]

If you intend, as I did, to move the found files, be aware that mv and cp do not preserve resource forks. You can use CpMac or MvMac if you've installed the Developer Tools, or you can use `ditto --rsrc`.

I'm no expert at this stuff, either. There are probably better ways (and definitely other ways), but maybe this will help you get off the ground. And since your described task is pretty similar to what I was trying to do, I'd certainly like to see what you end up with. (I've shelved my efforts for the moment, after allowing feature bloat (such as populating a MySQL database with archived file information for later searching) to push the project beyond my immediate means).

The good people at O'Reilly have a book called 'Learning the bash Shell'. The second edition covers bash version 2, which is what is on my 10.3.5 box. You can get the examples from the book here: <http://examples.oreilly.com/bash2/>. Perhaps those will provide more inspiration than my feeble attempts.

For the cron part, check out CronniX for a GUI if you're so inclined (<http://www.abstracture.de/cronnix>).

HTH,
Adam

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Somewhat OT (From: "John C. Welch" <email@hidden>)

  • Prev by Date: Re: get words of text fails
  • Next by Date: Re: Somewhat OT
  • Previous by thread: Somewhat OT
  • Next by thread: Re: Somewhat OT
  • Index(es):
    • Date
    • Thread