How to Run a Command on Many Files in Your Git Repository

Git getting you feeling crabby?

Occasionally it’s useful to run commands on files in your repository, or all files matching a pattern. This is possible by combining git ls-files and xargs.

For all files:

$ git ls-files | xargs <command>

Or for files with a given suffix:

$ git ls-files -- '*.<suffix>' | xargs <command>

A Deconstructed Example

For example, to get the line counts of all Python files:

$ git ls-files -- '*.py' | xargs wc -l
     255 example/
     138 example/
    3160 example/
   78123 total

Let’s take that apart.

You can take the line count of a file with wc and its -l flag:

$ wc -l example/
    7142 example/

git ls-files -- '*.py' lists all files known to Git that end in .py, in the current folder:

$ git ls-files -- '*.py'

xargs takes a list of files from its input, and runs the command you give it on them, in groups. So in the combination command, it receives the list of files from git ls-files, and runs wc -l on them in batches.

Running Fixer Tools

You can also use this techinque to run “fixer” tools. For example to run my tool django-upgrade on all Python files in your repository:

$ git ls-files -- '*.py' | xargs django-upgrade --target-version 4.0
Rewriting example/
Rewriting example/


May your mass edits not require mass reverts,


If your Django project’s long test runs bore you, I wrote a book that can help.

Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts: