Brain Phrye

code cooking diy fiction personal photos politics reviews tools

Xargs Script

This year it seems that everything that was ticking along just fine is suddenly breaking. Which is annoying. This week was a home git mirror.

My network connection is, at times, rather slow. Sometimes it even disappears. It’s therefore kind of nice if I have copies of code bases locally. I have a script that does this just fine and it’s worked for years.

Starting this year it started to bog down the server I ran it on. So I read up and it turns out that there are git settings that can cause it to use unlimited memory. Specifically the default settings. Sigh. Luckily there are some things you can do to fix that.

I started to write the fix this morning but a completely different emergency cropped up and I really quickly wrote the beginnings of a script and set it aside. The idea was that I’d do

find mirror -name '*.git' -print0 | xargs -0 -n 1 ./script

The script would cd into the the dir, run a series of git commands, report some stats and exit to do it all over for the next dir find runs it with. But because I was lazy I chucked the find ... | xargs bit in the script to remind me later on.

However when I looked at it at lunch time I realised I could put the whole thing in the single script. This is the rough framework of it:

me="$(realpath "$0")"
if [[ -n "$1" ]]; then
cd "$1"
run some commands here
find some/dir -name '*.git' -print0 | xargs -0 -n 1 "$me"

All in one single package. This little idiom makes more complicated xargs runs a bit simpler.