Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Wanted: script to touch folder

Wanted: script to touch folder
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2001
Status: Offline
Reply With Quote
Mar 23, 2006, 10:32 PM
 
I want to be able to set the modification date of a folder to the most recent modification date of the items it contains.

I'm trying to find a utility of any sort - anything from a shell script to a Contextual Menu Module will do - which can perform this little trick.
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Mar 24, 2006, 10:02 AM
 
     
Fresh-Faced Recruit
Join Date: Jul 2001
Status: Offline
Reply With Quote
Mar 24, 2006, 10:27 AM
 
Originally Posted by Mithras
I realize you're trying to be helpful by providing the absolute minimum of help, but telling me to help myself isn't very helpful.

Can anyone help me find a script or tool which can touch a folder's modification date, AUTOMATICALLY setting it to the most recent modification date of its contents? You know, a script or tool which has already been written for such a purpose? One which I can download and start using right away? I know this problem has to have been solved before, by shell scripters more capable than me.
     
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Mar 24, 2006, 11:18 AM
 
I wrote this one... it should get you started:
parentModDate2LatestChild
Code:
#!/bin/sh - # Script to change the modified date on a folder # to match that of its most recent visible file: if [ -d "$*" ]; then cd "$*"; fi theLatest= getLatest () { # FUNCTION TO GET LATEST FILE (OR FOLDER) IN A FOLDER: theChar= if [ "$1" = "folder" ]; then theChar='d'; else theChar='-'; fi theLatest=$(/bin/ls -ltT | /usr/bin/sed -ne '/^'${theChar}'/p' | /usr/bin/sed -ne '/alias/!p' | /usr/bin/sed -ne '1s/^'${theChar}'.*..:..:.. [0-9]\{4\} //p') # echo -n "latest $1 is: "; /bin/ls -ld "$theLatest" 2>/dev/null } ### main () echo "adjusting mod date on folder: $PWD" echo -n 'before: '; /usr/bin/stat -f %Sm . # getLatest file if [ -f "$theLatest" ] then /usr/bin/touch -mr "$theLatest" . echo -n 'after: '; /usr/bin/stat -f %Sm . echo; exit 0 else printf ' \e[4m%s\e[0m\n' "no 'visible' file to reference in $PWD" # getLatest folder if [ -d "$theLatest" ] then /usr/bin/touch -mr "$theLatest" . echo -n 'after: '; /usr/bin/stat -f %Sm . echo; exit 0 else printf '\e[7m%s\e[0m\n' "no file OR folder to reference in $PWD" echo; exit 1 fi fi exit $?
Because I use "stat" to echo some info, this requires Tiger.
Pather users could change it to "ls -lTd" or comment it out.

--

If you want to do an entire folder's depth, you'd call my tool
from another script... sorta like this:
fixAllSubfolderDates
Code:
#!/bin/sh - # if [ -d "$*" ]; then cd "$*"; fi # /usr/bin/find -dx . -type d -not -path "*/Contents*" -not -name "*.app" -print0 | /usr/bin/xargs -0 -n1 ~/bin/parentModDate2LatestChild # exit $?
(Last edited by Hal Itosis; Mar 24, 2006 at 12:07 PM. )
-HI-
     
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Mar 24, 2006, 11:20 AM
 
Code tags in this forum strips out tabs...
... and leading spaces too.
(Last edited by Hal Itosis; Mar 24, 2006 at 12:11 PM. )
-HI-
     
Fresh-Faced Recruit
Join Date: Jul 2001
Status: Offline
Reply With Quote
Mar 25, 2006, 08:39 PM
 
Originally Posted by Hal Itosis
I wrote this one... it should get you started:
Thanks a lot, Hal. That script was just the ticket.
     
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Mar 25, 2006, 10:56 PM
 
It may be subtle, but I'm filtering out aliases (line 11) because they seem
to (sometimes) get updated by the OS (or maybe it's File Buddy).

Anyway, I didn't want aliases to promote a folder to a later date than
it deserved... so I exclude (oh oh) things with "alias" in the name... which
means I'm missing some aliases (that don't have alias in their name), and
potentially excluding some valid files (if they do have alias in their name).

Oh well, like I said, it's a "starting" point. (delete line 11 and forget about it).
 
     
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Mar 30, 2006, 01:52 AM
 
Ran into an unexpected scenario: when fixing subfolder items in directories,
where either ownership or write access prevented changing modification dates.

No problem. We just prepend sudo... right?
$ sudo parentModDate2LatestChild
(or: sudo fixAllSubfolderDates)

Well, one weird thing with sudo and 'ls' is that -- no matter what options are
(or aren't) specified -- ALL hidden dot files (except the current and the parent
directory symbols) will still appear...

man ls:
-A     List all entries except for . and .. Always set for the superuser.

Therefore -- whenever sudo is employed -- that brings .DS_Stores (and many other
'invisibles') into the mix... which is not desirable for our (read: "my") purpose.

So here then is version 1.02, which includes some other tweaks... like a much
simpler sed statement (courtesy ls -1):

parentModDate2LatestChild
Code:
#!/bin/sh - # Script to change the modified date on a folder # to match that of its most recent visible file: PATH='/bin:/usr/bin' export PATH IFS=' ' if [ -d "$*" ]; then cd "$*"; fi theLatest= getLatest() { ### FUNCTION TO GET LATEST FILE (OR FOLDER) IN A FOLDER: theChar= if [ "$1" = "folder" ]; then theChar='!'; else theChar=''; fi theLatest=$(ls -1ptv | sed -e '/\/$/'${theChar}'d' -e '/^\./d' -e '/ alias$/d' -e q) echo -n "latest $1 is: "; ls -lhdTv "$theLatest" 2>/dev/null } ### main() echo "adjusting mod date on folder: $PWD" echo -n 'before: '; stat -f '%Sm' . getLatest file if [ -f "$theLatest" ]; then touch -mr "$theLatest" . echo -n 'after: '; stat -f '%Sm%n%n' . exit 0 else printf ' \e[4m%s\e[0m\n' "no 'visible' file to reference in $PWD" getLatest folder if [ -d "$theLatest" ]; then touch -mr "$theLatest" . echo -n 'after: '; stat -f '%Sm%n%n' . exit 0 else printf '\e[7m%s\e[0m\n\n' "no file OR folder to reference in $PWD" exit 1 fi fi exit $?
Whether or not you intend to run parentModDate2LatestChild with sudo, definitely get this
updated version and just dump the old one. (And I'm still using the 'stat' command here...
so Panther users need to read the note about that in my first post).

BTW - since the software/cgi here monkeys with our code...
note that, between the single quotes in the IFS= statement are:
IFS='<space><tab>
<newline>'
(Last edited by Hal Itosis; Mar 30, 2006 at 02:04 AM. )
-HI-
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:33 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2