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>'