Go Back   The macosxhints Forums > OS X Help Requests > UNIX - Newcomers



Reply
 
Thread Tools Rating: Thread Rating: 15 votes, 2.53 average. Display Modes
Old 10-08-2012, 05:40 AM   #1
benwiggy
League Commissioner
 
Join Date: Aug 2006
Posts: 5,039
Find, xargs, commands

I'm trying to do something really basic, but I just can't seem to get it right. I want to find a bunch of files, and then perform a command on them.
The find criterion is "Creation Date = 1970 1 Jan", so:
Code:
find * \! -newerBt 19700102
That seems to work. The command I want to run is:
Code:
SetFile -d "$(GetFileInfo -m ${a})" ${a}
(Set the Creation Date to the Modified Date.)

So I tried:
Code:
for a in $(find * \! -newerBt 19700102); do SetFile -d "$(GetFileInfo -m ${a})" ${a}; done
But I get "GetFileInfo: could not refer to file (-43)"

When I try replacing the action with "echo", I get the filenames divided by spaces, despite my best efforts to avoid that. I then tried:
Code:
for a in $(find * \! -newerBt 19700102 -print0); do xargs -0 echo "$a"; done
which just hangs.

I tried
Code:
find * \! -newerBt 19700102 -print0 | xargs -0 GetFileInfo -m
and I get a "usage" error for GetFileInfo

I'm probably making a complete mess of this. I've flicked through some Shell script sample sites to find something similar, but with limited success.

Any help greatly appreciated.

Last edited by benwiggy; 10-08-2012 at 05:43 AM.
benwiggy is offline   Reply With Quote
Old 10-08-2012, 06:56 AM   #2
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
Try this:
Code:
find * \! -newerBt 19700102 -exec SetFile -d "$(GetFileInfo -m {})" {} \;
SirDice is offline   Reply With Quote
Old 10-08-2012, 07:10 AM   #3
benwiggy
League Commissioner
 
Join Date: Aug 2006
Posts: 5,039
Thanks, but I get "ERROR: invalid date/time".

What's weird is that "GetFileInfo -m <file>" works on its own as a command in the Terminal, but as soon as you use wildcards, it doesn't. So "GetFileInfo -m *" doesn't work.
benwiggy is offline   Reply With Quote
Old 10-08-2012, 07:23 AM   #4
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
All roads lead to Rome

Code:
find * \! -newerBt 19700102 | while read file; do
 echo ${file}
 info = `GetFileInfo -m ${file}`
 SetFile -d "${info}"
done
SirDice is offline   Reply With Quote
Old 10-08-2012, 07:28 AM   #5
benwiggy
League Commissioner
 
Join Date: Aug 2006
Posts: 5,039
Quote:
Originally Posted by SirDice
All roads lead to Rome

Not all, in this case!

usage: GetFileInfo [-P] [-a[<attrib-letter>] | -t | -c | -d | -m] <path>
ERROR: invalid date/time

I was wondering whether there was a way to do it without the Apple-specific, Xcode-only Apple commands.
I can get the date with:
Code:
stat -f %Sm -t %Y%m%d
But touch doesn't seem to change the creation date, despite plenty of websites claiming that it does.
benwiggy is offline   Reply With Quote
Old 10-09-2012, 12:08 AM   #6
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,008
Quote:
Originally Posted by SirDice
All roads lead to Rome

Code:
find * \! -newerBt 19700102 | while read file; do
 echo ${file}
 info = `GetFileInfo -m ${file}`
 SetFile -d "${info}"
done

You can't put whitespace around the = in an assignment. (I've also replaced backticks with a $( ... ) construct, just because I find it more readable. It's also more flexible, in that $( ... ) can be nested without horrendous quoting.)

Plus, you forgot to quote the filename, which might contain whitespace or other shell-meaningful characters.

And you left the filename off the SetFile command.

Code:
find * \! -newerBt 19700102 | while read file; do
 echo "$file"
 info=$(GetFileInfo -m "$file")
 SetFile -d "$info" "$file"
done
ganbustein is offline   Reply With Quote
Old 10-09-2012, 02:29 AM   #7
benwiggy
League Commissioner
 
Join Date: Aug 2006
Posts: 5,039
Great. That works just fine.

Thanks.
benwiggy is offline   Reply With Quote
Old 10-09-2012, 04:32 AM   #8
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
I typed it from the top of my head.
SirDice is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting 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

Forum Jump



All times are GMT -5. The time now is 04:58 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.