# Using `in` to run a command in multiple directories

Recently, I found a cool Go utility named [`in`](https://github.com/xyproto/in) that allows you to run a command in a directory you specify. When you run the `in` command with a directory that doesn't exist it first creates the directory and runs the command.
So this command is cool because it allows you to run a command in a specific directory on the command-line without first navigating to it (via `cd`). 

The idea behind the tool is to allow you to do this:

```sh
$ in build cmake ..
```

Instead of 
```sh
$ mkdir -p build
$ cd build
$ cmake ..
```

## Adding a feature to `in`

When I found this tool I knew I had to add another feature to it to accomplish something I have needed for a while; a command-line tool to run the same command in multiple project directories at once. 

I sent in a [Pull-Request](https://github.com/xyproto/in/pull/1) to add glob support to enable `in` to be used to run a command in multiple directories. I was pleased to see the PR merged 🎉 So this functionality is available to anyone else that comes across `in`.

So, let's see what we can do with this new feature:

**Compile all maven projects**

```sh
$ in "./**/*pom.xml" mvn clean compile
```

**View last N commits for all projects in a directory**

This works if the directory has subdirectories that are Git repositories.

```sh
$ in "./*" git log --oneline -n 3
```

**Pull refs for all git repos in a directory**

Like the previous example, this works if the directory has subdirectories that are Git repositories.

```sh
$ in "./*" git pull --all
```

**Run go fmt on all directories containing Go files**

```sh
$ in "./**/*.go" go fmt
```

**Creating GitHub actions workflow directories in multiple directories**

**With Powershell**

```sh
$ in ".\oss\*" powershell "mkdir .github/workflow"
```

**With Bash**

```sh
$ in "./oss/*" mkdir -p .github/workflow
```

## Installing and using `in`

In order to use `in` you will currently need to build it locally. Fortunately, the command does not have external dependencies, at the time of writing, so all you will need is the Go toolchain. [Download Go here](https://go.dev/dl)

```sh
$ git clone https://github.com/xyproto/in
$ cd in
$ go build
```

Personally, I build the binary as `indir` as I think that makes more sense despite being more characters and also works much better with Powershell :). If you want to use that similar approach you can build it like so:

```sh
$ go build -o indir main.go
```

Or like this on Windows

```sh
$ go build -o indir.exe main.go
```

When you build it like that you can place the command on your `$PATH` and call the command as follows:

```sh
$ indir "./**/*pom.xml" mvn clean compile
```

## Conclusion

`in` is a great tool which has saved me the trouble of writing multiple single-use case commands. Open-Source software is awesome and contributing to something that I needed and use feels good. 
Moving on, I would like to improve the documentation to make sure people understand the behavior of the tool especially with the glob feature and add some notes on using the tool in a Windows environment.
