Question

I was trying to build a quick script to find all the git repos under a directory and sequentially "git pull" each one.

This is what I found so far:

find ~/ -name ".git" -type d | sed 's,/*[^/]\+/*$,,' | xargs -L1 bash -c 'cd "$1" && git pull' _

If pasted into a terminal, this will work exactly as it's intended. However, if I make this into an alias in my .bashrc file:

alias gpa="find ~/ -name ".git" -type d | sed 's,/*[^/]\+/*$,,' | xargs -L1 bash -c 'cd "$1" && git pull' _"

The command doesn't work. I modified it in an attempt to get it to print what the subshell launched by xargs is receiving:

alias printgpa="find ~/ -name ".git" -type d | sed 's,/*[^/]\+/*$,,' | xargs -L1 bash -c 'echo "$1"' _"

When run, each subshell prints a newline, but nothing else.

Can anyone answer why this is happening? My gut feeling says that it's a problem with my syntax in the alias, but I don't know exactly what's going on.

Was it helpful?

Solution

The problem is that $1 is getting substituted when you define the alias, rather than when you run it. To prevent that, you need to quote the $, either by using a backslash, or by using some single-quotes. For example:

alias printgpa='find ~/ -name .git -type d | sed '\''s,/*[^/]\+/*$,,'\'' | xargs -L1 bash -c '\''echo "$1"'\'' _'

OTHER TIPS

Maybe it is easier to read when you use GNU Parallel:

alias gpa="find ~/ -name .git -type d | parallel 'cd {//} && git pull'"

Plus you get the added benefit of getting more gits to pull in parallel.

It takes literally 10 seconds to install GNU Parallel:

wget pi.dk/3 -qO - | sh -x

Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top