Questions tagged [glob]

globs, more properly called [shell] patterns, and also known as wildcard expressions, are instances of a pattern-matching language used in many, esp. POSIX-like shells, to match filenames and strings: e.g., *.c is a glob for C source files and matches any file whose suffix (extension) is .c

Glob patterns are distantly related to regular expressions, but have simpler, incompatible syntax and offer fewer features.

Their typical use is to match multiple filenames or paths based on an abstract pattern; e.g.:

  • *.cpp
  • log-user*-[0-9][0-9].gz

A major difference between globs and regular expressions (tag: ) is there are far fewer meta-characters for globbing, and there are no duplication symbols (quantifiers). Most notably, * and ? by themselves match any (possibly empty) sequence of characters and any single character, respectively.

Wikipedia has a page on glob patterns.

On Unix systems, the POSIX pattern notation defines the notation for POSIX-compatible shells.

Generally, it is the shell itself that processes a glob pattern specified as an unquoted argument to a command: it expands it to all matching filenames and passes them as individual arguments to the command given, a process known as filename or pathname expansion.

Note that use of patterns in POSIX-like shells is not restricted to filename expansion: they are also used in parameter expansions (e.g., ${HOME##*/} to strip the directory path from a path) and string-matching operations (such as with case ... esac).

3201 questions
48
votes
6 answers

Case-insensitive Glob on zsh/bash

I need to list all files whose names start with 'SomeLongString'. But the case of 'SomeLongString' can vary. How? I am using zsh, but a bash solution is also welcome.
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
47
votes
5 answers

Quicker to os.walk or glob?

I'm messing around with file lookups in python on a large hard disk. I've been looking at os.walk and glob. I usually use os.walk as I find it much neater and seems to be quicker (for usual size directories). Has anyone got any experience with…
joedborg
  • 17,651
  • 32
  • 84
  • 118
47
votes
2 answers

GitHub Actions run on push to all branches

It's easy to run a GitHub Action on any push or pull request: # Triggers the workflow on push or pull request events on: [push, pull_request] But what if I want to restrict runs to pull requests opened against specific base refs, while allowing…
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
47
votes
7 answers

Glob search files in date order?

I have this line of code in my python script. It searches all the files in in a particular directory for * cycle *.log. for searchedfile in glob.glob("*cycle*.log"): This works perfectly, however when I run my script to a network location it does…
Jason Rogers
  • 667
  • 1
  • 6
  • 19
45
votes
5 answers

Ignore files with names starting with 'output'

I have a program that generates text files output1.txt, output2.txt, output3.txt, etc.. I want Git to ignore these files. I can't block text files, as I have some text files that shouldn't be ignored. Also, the files are dynamically generated (there…
user4302594
44
votes
1 answer

Loop over results from Path.glob() (Pathlib)

I'm struggling with the result of the Path.glob() method of the Pathlib module in Python 3.6. from pathlib import Path dir = Path.cwd() files = dir.glob('*.txt') print(list(files)) >> [WindowsPath('C:/whatever/file1.txt'),…
keyx
  • 567
  • 1
  • 4
  • 5
44
votes
2 answers

What does ** mean in a path?

ive been setting up Grunt for my web app to auto build it and im seeing paths like /path/to/file/**/*.js i understand what one wildcard means, but what does 2 in a row mean?
Kev
  • 705
  • 1
  • 5
  • 10
44
votes
8 answers

Is There A Way To glob() Only Files?

I know that glob can look for all files or only all directories inside a folder : echo "All files:\n"; $all = glob("/*"); var_dump($all); echo "Only directories\n"; $dirs = glob("/*", GLOB_ONLYDIR); var_dump($dirs); But I didn't found something to…
Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
42
votes
2 answers

How can I merge multiple lists of files together with CMake?

I have a project built with CMake that needs to copy some resources to the destination folder. Currently I use this code: file(GLOB files "path/to/files/*") foreach(file ${files}) ADD_CUSTOM_COMMAND( TARGET MyProject POST_BUILD …
Calvin
  • 2,872
  • 2
  • 21
  • 31
40
votes
4 answers

glob and bracket characters ('[]')

/Users/smcho/Desktop/bracket/[10,20] directory has "abc.txt", but when I run this Python code import glob import os.path path1 = "/Users/smcho/Desktop/bracket/\[10,20\]" pathName = os.path.join(path1, "*.txt") print glob.glob(pathName) It returns…
prosseek
  • 182,215
  • 215
  • 566
  • 871
38
votes
5 answers

Simple glob in C++ on unix system?

I want to retrieve all the matching paths following this pattern in a vector: "/some/path/img*.png" How can I simply do that ?
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
37
votes
4 answers

Assign results of globbing to a variable in Bash

My colleague, Ryan, came to me with a bug in his Bash script, and I identified the problem with this test: $ mkdir ryan $ mkdir ryan/smells-bad $ FOO=ryan/smells-* $ echo $FOO ryan/smells-bad $ touch $FOO/rotten_eggs touch: cannot touch…
Rob Fisher
  • 945
  • 1
  • 9
  • 20
35
votes
2 answers

How to loop through all the files located under a certain path in zsh?

Here's what I have so far: for file in $(find /path/to/directory -type f); echo $file; done but I get this error: zsh: parse error near `done'
Paul Baltescu
  • 2,095
  • 4
  • 25
  • 30
35
votes
7 answers

how to `git ls-files` for just one directory level.

I'm using msysgit (1.7.9), and I'm looking for the right invocation of the git ls-files command to show just the (tracked) files and directories at the current level, either from the index, or the current working directory if that's easier.…
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
31
votes
1 answer

How do I test whether a string would match a glob in Ruby?

Without hitting the filesystem, is it possible to see whether the glob "foo*" would match "food" in Ruby? Background: one of my scripts produce files, and I'd like to unit test that other scripts would be able to detect such files with their current…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338