Home › Forums › OS X Server and Client Discussion › Questions and Answers › Simple Find and Replace script
- This topic has 2 replies, 3 voices, and was last updated 18 years, 4 months ago by
filipp.
-
AuthorPosts
-
December 14, 2006 at 4:13 am #367829
Flash
ParticipantAll I want to do is find all folders of a particular name on a network homedir volume, then replace them with my own folder.
I have several potential applications for such a find and replace script:
1. What if I want to replace all user bookmarks.plist files with a specific “school endorsed” bookmarks file?
2. Or finding/replacing every user’s Caches folder with a symlink that redirects to a local folder on client machines, thereby taking enormous burden off of the server.
Countless uses for such a find and replace script.
Automator can easily find all of the files or folders, but has no obvious mechanism for replacing them en masse.
Any advice much appreciated.
ADC support recommended buying an Applescript book which I did – it was worthless. Thanks, such advice is well worth the $195 support incident (oozing sarcasm).
December 17, 2006 at 11:49 am #367848filipp
ParticipantI thought this was a really neat idea. While it’s probably possible to do this with AppleScript, it’s really the sort of thing that Bash scripting is perfect for. You can basically do something with the super-tool “find”, with it’s exec – parameter:
[code]find someplace -exec dosomething {} \;[/code]
But I got so excited that I wrote [url=http://homepage.mac.com/filipp/tarkvara/files/freplace.sh]this little Bash script[/url]. I think it does what you had in mind. Allow me to demonstrate:
[code]> Usage: freplace.sh [-sb] [-o owner:group] -d indir target replacement
> touch test/a
echo “testing” > b
[/code]
The principle is “replace [i]target[/i] in [i]directory[/i] -d with [i]replacement[/i]”. Let’s replace “test/a” with “b”:
[code]
> ./freplace.sh -d test/ a b
*** Replacing “/Users/filipp/Documents/Code/test/a” > “/Users/filipp/Documents/Code/b”
> cat test/a
testing[/code]
It can also replace search results with a symlink with the same name that points to “replacement” with the “-s” switch:
[code]> ./freplace.sh -s -d test/ a b
> ls -l test/a
lrwxr-xr-x 1 filipp filipp 30 Dec 17 01:41 test/a -> /Users/filipp/Documents/Code/b[/code]
As a precaution, I added the ‘b’ switch that moves that search result to ‘filename.old’ before replacing (sc “backup”). Finally it’s also possible to set the ownership of the replaced file with -o username:groupname, like so:
[code]> sudo ./freplace.sh -o nobody:www -d test/ a b
> ls -l test/a
-rw-r–r– 1 nobody www 8 Dec 17 01:49 test/a[/code]
The whole thing is recursive and you can use wildcards in queries. Can also replace directories with files or symlinks and vice versa. Handle with care! 😉 -
AuthorPosts
- You must be logged in to reply to this topic.
Comments are closed