Unix-style pathname pattern expansion
Table of Contents
Quick Start
- This library is available in two flavors:
- Two file version:
glob.h
andglob.cpp
- Single header file version in
single_include/
- Two file version:
- No external dependencies - just the standard library
- Requires C++17
std::filesystem
- If you can't use
C++17
, you can integrate gulrak/filesystem with minimal effort.
- If you can't use
- MIT License
Build Library and Standalone Sample
cmake -Hall -Bbuild
cmake --build build
# run standalone `glob` sample
./build/standalone/glob --help
Usage
// Match on a single pattern
for (auto& p : glob::glob("~/.b*")) { // e.g., .bash_history, .bashrc
// do something with `p`
}
// Match on multiple patterns
for (auto& p : glob::glob({"*.png", "*.jpg"})) { // e.g., foo.png, bar.jpg
// do something with `p`
}
// Match recursively with `rglob`
for (auto& p : glob::rglob("**/*.hpp")) { // e.g., include/foo.hpp, include/foo/bar.hpp
// do something with `p`
}
API
/// e.g., glob("*.hpp")
/// e.g., glob("**/*.cpp")
/// e.g., glob("test_files_02/[0-9].txt")
/// e.g., glob("/usr/local/include/nc*.h")
/// e.g., glob("test_files_02/?.txt")
vector<filesystem::path> glob(string pathname);
/// Globs recursively
/// e.g., rglob("Documents/Projects/Foo/**/*.hpp")
/// e.g., rglob("test_files_02/*[0-9].txt")
vector<filesystem::path> rglob(string pathname);
There are also two convenience functions to glob
on a list of patterns:
/// e.g., glob({"*.png", "*.jpg"})
vector<filesystem::path> glob(vector<string> pathnames);
/// Globs recursively
/// e.g., rglob({"**/*.h", "**/*.hpp", "**/*.cpp"})
vector<filesystem::path> rglob(vector<string> pathnames);
Wildcards
Wildcard | Matches | Example |
---|---|---|
* |
any characters | *.txt matches all files with the txt extension |
? |
any one character | ??? matches files with 3 characters long |
[] |
any character listed in the brackets | [ABC]* matches files starting with A,B or C |
[-] |
any character in the range listed in brackets | [A-Z]* matches files starting with capital letters |
[!] |
any character listed in the brackets | [!ABC]* matches files that do not start with A,B or C |
Examples
The following examples use the standalone sample that is part of this repository to illustrate the library functionality.
foo@bar:~$ ./build/standalone/glob -h
Run glob to find all the pathnames matching a specified pattern
Usage:
./build/standalone/glob [OPTION...]
-h, --help Show help
-v, --version Print the current version number
-r, --recursive Run glob recursively
-i, --input arg Patterns to match
Match file extensions
foo@bar:~$ tree
.
├── include
│ └── foo
│ ├── bar.hpp
│ ├── baz.hpp
│ └── foo.hpp
└── test
├── bar.cpp
├── doctest.hpp
├── foo.cpp
└── main.cpp
3 directories, 7 files
foo@bar:~$ ./glob -i "**/*.hpp"
"test/doctest.hpp"
foo@bar:~$ ./glob -i "**/**/*.hpp"
"include/foo/baz.hpp"
"include/foo/foo.hpp"
"include/foo/bar.hpp"
NOTE If you run glob recursively, i.e., using rglob
:
foo@bar:~$ ./glob -r -i "**/*.hpp"
"test/doctest.hpp"
"include/foo/baz.hpp"
"include/foo/foo.hpp"
"include/foo/bar.hpp"
Match files in absolute pathnames
foo@bar:~$ ./glob -i '/usr/local/include/nc*.h'
"/usr/local/include/ncCheck.h"
"/usr/local/include/ncGroupAtt.h"
"/usr/local/include/ncUshort.h"
"/usr/local/include/ncByte.h"
"/usr/local/include/ncString.h"
"/usr/local/include/ncUint64.h"
"/usr/local/include/ncGroup.h"
"/usr/local/include/ncUbyte.h"
"/usr/local/include/ncvalues.h"
"/usr/local/include/ncInt.h"
"/usr/local/include/ncAtt.h"
"/usr/local/include/ncVar.h"
"/usr/local/include/ncUint.h"
Wildcards: Match a range of characters listed in brackets ('[]')
foo@bar:~$ ls test_files_02
1.txt 2.txt 3.txt 4.txt
foo@bar:~$ ./glob -i 'test_files_02/[0-9].txt'
"test_files_02/4.txt"
"test_files_02/3.txt"
"test_files_02/2.txt"
"test_files_02/1.txt"
foo@bar:~$ ./glob -i 'test_files_02/[1-2]*'
"test_files_02/2.txt"
"test_files_02/1.txt"
foo@bar:~$ ls test_files_03
file1.txt file2.txt file3.txt file4.txt
foo@bar:~$ ./glob -i 'test_files_03/file[0-9].*'
"test_files_03/file2.txt"
"test_files_03/file3.txt"
"test_files_03/file1.txt"
"test_files_03/file4.txt"
Exclude files from the matching
foo@bar:~$ ls test_files_01
__init__.py bar.py foo.py
foo@bar:~$ ./glob -i 'test_files_01/*[!__init__].py'
"test_files_01/bar.py"
"test_files_01/foo.py"
foo@bar:~$ ./glob -i 'test_files_01/*[!__init__][!bar].py'
"test_files_01/foo.py"
foo@bar:~$ ./glob -i 'test_files_01/[!_]*.py'
"test_files_01/bar.py"
"test_files_01/foo.py"
Wildcards: Match any one character with question mark ('?')
foo@bar:~$ ls test_files_02
1.txt 2.txt 3.txt 4.txt
foo@bar:~$ ./glob -i 'test_files_02/?.txt'
"test_files_02/4.txt"
"test_files_02/3.txt"
"test_files_02/2.txt"
"test_files_02/1.txt"
foo@bar:~$ ls test_files_03
file1.txt file2.txt file3.txt file4.txt
foo@bar:~$ ./glob -i 'test_files_03/????[3-4].txt'
"test_files_03/file3.txt"
"test_files_03/file4.txt"
Case sensitivity
glob
matching is case-sensitive:
foo@bar:~$ ls test_files_05
file1.png file2.png file3.PNG file4.PNG
foo@bar:~$ ./glob -i 'test_files_05/*.png'
"test_files_05/file2.png"
"test_files_05/file1.png"
foo@bar:~$ ./glob -i 'test_files_05/*.PNG'
"test_files_05/file3.PNG"
"test_files_05/file4.PNG"
foo@bar:~$ ./glob -i "test_files_05/*.png","test_files_05/*.PNG"
"test_files_05/file2.png"
"test_files_05/file1.png"
"test_files_05/file3.PNG"
"test_files_05/file4.PNG"
Tilde expansion
foo@bar:~$ ./glob -i "~/.b*"
"/Users/pranav/.bashrc"
"/Users/pranav/.bash_sessions"
"/Users/pranav/.bash_profile"
"/Users/pranav/.bash_history"
foo@bar:~$ ./glob -i "~/Documents/Projects/glob/**/glob/*.h"
"/Users/pranav/Documents/Projects/glob/include/glob/glob.h"
Contributing
Contributions are welcome, have a look at the CONTRIBUTING.md document for more information.
License
The project is available under the MIT license.
version | 0.0.1+1 |
---|---|
license | MIT |
repository | https://pkg.cppget.org/1/alpha |
download | libglob-0.0.1+1.tar.gz |
sha256 | a245ef8adb6b91c89526cfeedc56fef9482c7dd85e9c355bb9dad9c841fe43b9 |
project | glob |
---|---|
url | github.com/p-ranav/glob |
Requires (1)
c++17 |
Reviews
fail | 0 |
---|---|
pass | 1 |
Builds
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_ubuntu_24.04-gcc_13-bindist |
timestamp | 2025-08-22 18:30:06 UTC (16:58:45 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64-static_O2 |
timestamp | 2025-08-22 18:13:02 UTC (17:15:49 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64-O2 |
timestamp | 2025-08-22 18:11:13 UTC (17:17:37 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64 |
timestamp | 2025-08-22 18:06:29 UTC (17:22:21 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-static_O3 |
timestamp | 2025-08-22 17:57:13 UTC (17:31:38 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_12-bindist |
timestamp | 2025-08-22 17:56:02 UTC (17:32:48 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd13.3 |
tgt config | freebsd_13-clang_17 |
timestamp | 2025-08-22 17:55:45 UTC (17:33:06 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-ndebug_O3 |
timestamp | 2025-08-22 17:54:06 UTC (17:34:45 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-O3 |
timestamp | 2025-08-22 17:52:36 UTC (17:36:15 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14 |
timestamp | 2025-08-22 17:51:53 UTC (17:36:58 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18-static_O3 |
timestamp | 2025-08-22 17:38:25 UTC (17:50:25 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18-O3 |
timestamp | 2025-08-22 17:34:59 UTC (17:53:52 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18 |
timestamp | 2025-08-22 17:33:36 UTC (17:55:14 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_17_libc++ |
timestamp | 2025-08-22 17:33:36 UTC (17:55:15 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_17 |
timestamp | 2025-08-22 17:32:20 UTC (17:56:31 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1 |
timestamp | 2025-08-22 17:31:09 UTC (17:57:41 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_17_libc++ |
timestamp | 2025-08-22 11:27:30 UTC (01 00:01:21 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_17 |
timestamp | 2025-08-22 11:27:27 UTC (01 00:01:24 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13 |
timestamp | 2025-08-22 10:43:12 UTC (01 00:45:38 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_40-gcc_14-bindist |
timestamp | 2025-08-22 03:26:24 UTC (01 08:02:27 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-O3 |
timestamp | 2025-08-21 02:02:59 UTC (02 09:25:52 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-static_O3 |
timestamp | 2025-08-21 02:00:42 UTC (02 09:28:09 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++ |
timestamp | 2025-08-21 01:59:27 UTC (02 09:29:24 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18-O3 |
timestamp | 2025-08-21 01:58:15 UTC (02 09:30:35 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18-static_O3 |
timestamp | 2025-08-21 01:57:41 UTC (02 09:31:10 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18 |
timestamp | 2025-08-21 01:56:44 UTC (02 09:32:06 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-static_O3 |
timestamp | 2025-08-20 17:06:54 UTC (02 18:21:56 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-ndebug_O3 |
timestamp | 2025-08-20 17:05:25 UTC (02 18:23:25 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-O3 |
timestamp | 2025-08-20 17:03:15 UTC (02 18:25:36 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14 |
timestamp | 2025-08-20 17:02:34 UTC (02 18:26:16 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10-O2 |
timestamp | 2025-08-20 16:20:00 UTC (02 19:08:50 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10-static_O2 |
timestamp | 2025-08-20 16:13:13 UTC (02 19:15:37 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10 |
timestamp | 2025-08-20 16:05:20 UTC (02 19:23:31 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10-static_O2 |
timestamp | 2025-08-20 14:14:57 UTC (02 21:13:54 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10-O2 |
timestamp | 2025-08-20 14:11:50 UTC (02 21:17:01 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_17_msvc_msvc_17.10 |
timestamp | 2025-08-20 14:10:59 UTC (02 21:17:51 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8-O2 |
timestamp | 2025-08-20 14:10:15 UTC (02 21:18:36 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10 |
timestamp | 2025-08-20 14:10:02 UTC (02 21:18:48 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8 |
timestamp | 2025-08-20 14:06:52 UTC (02 21:21:58 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8-static_O2 |
timestamp | 2025-08-20 13:47:16 UTC (02 21:41:34 days ago) |
result | warning (update) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++ |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_39-gcc_13-bindist |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew-static_O3 |
result | unbuilt |