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
1004
votes
28 answers

How to use to find files recursively?

I would like to list all files recursively in a directory. I currently have a directory structure like this: src/main.c src/dir/file1.c src/another-dir/file2.c src/another-dir/nested/files/file3.c I've tried to do the following: from glob import…
Ben Gartner
  • 14,413
  • 10
  • 36
  • 34
800
votes
6 answers

How to loop over files in directory and change path and add suffix to filename

I need to write a script that starts my program with different arguments. I start my program with: ./MyProgram.exe Data/data1.txt [Logs/data1_Log.txt]. Here is the pseudocode for what I want to do: for each filename in /Data do for int i = 0, i =…
Dobrobobr
  • 8,216
  • 4
  • 17
  • 18
423
votes
14 answers

Get a filtered list of files in a directory

I am trying to get a list of files in a directory using Python, but I do not want a list of ALL the files. What I essentially want is the ability to do something like the following but using Python and not executing ls. ls 145592*.jpg If there is…
mhost
  • 6,930
  • 5
  • 38
  • 45
382
votes
11 answers

How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

Say I want to copy the contents of a directory excluding files and folders whose names contain the word 'Music'. cp [exclude-matches] *Music* /target_directory What should go in place of [exclude-matches] to accomplish this?
user4812
  • 5,942
  • 9
  • 30
  • 35
364
votes
18 answers

Deleting all files from a folder using PHP?

For example I had a folder called `Temp' and I wanted to delete or flush all files from this folder using PHP. Could I do this?
getaway
  • 8,792
  • 22
  • 64
  • 94
360
votes
30 answers

How to count the number of files in a directory using Python

How do I count only the files in a directory? This counts the directory itself as a file: len(glob.glob('*'))
prosseek
  • 182,215
  • 215
  • 566
  • 871
324
votes
20 answers

Test whether a glob has any matches in Bash

If I want to check for the existence of a single file, I can test for it using test -e filename or [ -e filename ]. Supposing I have a glob and I want to know whether any files exist whose names match the glob. The glob can match 0 files (in which…
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
317
votes
12 answers

How are glob.glob()'s return values ordered?

I have written the following Python code: #!/usr/bin/python # -*- coding: utf-8 -*- import os, glob path = '/home/my/path' for infile in glob.glob( os.path.join(path, '*.png') ): print infile Now I get…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
246
votes
40 answers

Python glob multiple filetypes

Is there a better way to use glob.glob in python to get a list of multiple file types such as .txt, .mdown, and .markdown? Right now I have something like this: projectFiles1 = glob.glob( os.path.join(projectDir, '*.txt') ) projectFiles2 =…
Raptrex
  • 3,915
  • 10
  • 49
  • 61
217
votes
13 answers

Recursively add files by pattern

How do I recursively add files by a pattern (or glob) located in different directories? For example, I'd like to add A/B/C/foo.java and D/E/F/bar.java (and several other java files) with one command: git add '*.java' Unfortunately, that doesn't…
Michel Krämer
  • 14,197
  • 5
  • 32
  • 32
202
votes
17 answers

Using .gitignore to ignore everything but specific directories

My issue is that I have a bunch of WordPress websites in my git repo, of which I want to selectively commit only the content of my themes folders, while ignoring the rest of the redundant files found in WordPress. I've used .gitignore files to…
Yarin
  • 173,523
  • 149
  • 402
  • 512
198
votes
14 answers

Move all files except one

How can I move all files except one? I am looking for something like: 'mv ~/Linux/Old/!Tux.png ~/Linux/New/' where I move old stuff to new stuff -folder except Tux.png. !-sign represents a negation. Is there some tool for the job?
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
189
votes
12 answers

glob exclude pattern

I have a directory with a bunch of files inside: eee2314, asd3442 ... and eph. I want to exclude all files that start with eph with the glob function. How can I do it?
Anastasios Andronidis
  • 6,310
  • 4
  • 30
  • 53
187
votes
7 answers

Loop through all the files with a specific extension

for i in $(ls);do if [ $i = '*.java' ];then echo "I do something with the file $i" fi done I want to loop through each file in the current folder and check if it matches a specific extension. The code above doesn't work, do you know…
AR89
  • 3,548
  • 7
  • 31
  • 46
169
votes
4 answers

What does the double-asterisk (**) wildcard mean?

I've tried the following command but I don't understand the results: ls ** What does ** mean? How should I use it?
Mike Blair
  • 1,821
  • 2
  • 11
  • 11
1
2 3
99 100