globcat.sh
is a tiny little shell script that concatenates multiple files together, as specified by a glob pattern, and sends them to standard output.
I personally use it to send the source code of multiple files, from multiple directories, to ChatGPT, typically in a single prompt.
Absolute file paths are prepended to each section of the output, making it easy to give context to ChatGPT when asking questions such as, "help me write tests for this."
File: /path/to/docs/example.md
-------------------------------------
# Example Markdown
This is a sample markdown file.
=====================================
File: /path/to/src/example.txt
-------------------------------------
This is a sample text file.
=====================================
./globcat.sh [-e globs] [-d directories]
-
-e globs
:- A comma-separated list of file patterns (globs) to search for.
- Example:
*.md,*.txt
-
-d directories
:- A comma-separated list of directories to search within.
- Directories can be absolute paths or relative to the current directory.
- If no directories are specified, the current directory is used by default.
./globcat.sh -e '*.md,*.txt' -d './docs,./src'
This command will search for files with extensions .md
and .txt
within the docs
and src
directories and display their contents.
- Parsing Options: The script parses the provided globs and directories.
- Default Directory: If no directories are provided, it defaults to the current directory.
- Absolute Paths: Converts relative directory paths to absolute paths.
- File Search and Display: Recursively searches for files matching the specified globs in each directory, sorts the results, and displays their contents.
- Bash (version 3.2 or later [earlier versions may work]) or other compatible Unix-like shells (e.g.,
ksh
,zsh
). - Unix-like operating system (Linux, macOS, etc.)
- Download
globcat.sh
. - Make the script executable:
chmod +x globcat.sh
- Run the script using the usage instructions above.
Follow the preceding instructions for chmod +x
and then run the following:
sudo ln -s $(pwd)/globcat.sh /usr/local/bin/globcat
Now globcat
will be a system command that you can run from any directory.
Using Docker for testing ensures that the absolute file paths are handled consistently across different environments. This approach is particularly useful to ensure that the script behaves as expected when dealing with absolute paths.
- Build the Docker Image
Navigate to the root directory of your project (where globcat.sh
is located) and build the Docker image using the following command:
docker build -t globcat-test -f test.docker/Dockerfile .
- Run the Docker Container
Run the Docker container to execute the tests:
docker run --rm globcat-test
Change the parameters as needed.
Running scripts directly from GitHub is generally not an advisable thing to do, by the way, but here are the instructions on how to do it anyway:
curl -sL https://raw.githubusercontent.com/jzombie/globcat.sh/main/globcat.sh | bash -s -- -e '*.md,*.txt' -d '.'
If you have suggestions for improvements or find any issues, please feel free to submit a pull request or open an issue on the GitHub repository.
MIT License. Copyright (c) Jeremy Harris.