globfree - Linux
Overview
globfree is a command used to perform filename pattern matching and wildcard expansion. It evaluates shell globs and patterns and outputs a list of matching files and directories. Globs are expressions that match files based on criteria such as name, extension, or modification time.
Syntax
globfree [-a] [-cdfglLmrR] [-f <format>] [-i <in-path>] [-o <out-path>] [-p <in-file>] [-w <out-file>] [-z <separator>] [<glob-pattern>]
Options/Flags
- -a: Do not sort output.
- -c: Count matching files instead of printing names.
- -d: Match directories only.
- -f
: Specify output format for each line (options: ‘%p’, ‘%f’, ‘%L’, ‘%N’). - -g: Ignore case when matching.
- -i
: Read globs from a file. - -l: List paths in long format.
- -L: Follow symbolic links.
- -m: Allow multiple matches per line.
- -o
: Write output to a file. - -p
: Read input from a file. - -r: Match recursively.
- -R: Resolve symbolic links.
- -w
: Write output to a file (symlink safe). - -z
: Specify output separator (default: newline).
Examples
Simple pattern matching
globfree *.txt
Outputs a list of all files ending with ".txt".
Complex pattern matching
globfree -i glob-patterns.txt | grep -v 'readme.txt' | xargs rm
Reads glob patterns from a file, excludes "readme.txt", and deletes matching files.
Counting matching files
globfree -c *.txt
Outputs the number of files ending with ".txt".
Common Issues
- No output: Ensure that the glob pattern matches existing files. Check for typos or incorrect paths.
- Unexpected output: Verify the output format using the
-f
option. Use-a
to disable sorting and see the exact matches. - Permission denied: Ensure that you have the necessary permissions to access the specified files.
Integration
- Shell scripts: Use globfree to perform wildcard expansion and dynamically generate lists of files for processing.
- Find command: Combine globfree with find for advanced pattern matching and filtering.
- xargs command: Pipe the output of globfree to xargs to execute commands on matching files.
Related Commands
- find(1) – Search for files based on criteria
- xargs(1) – Execute commands on input lines
- bash(1) – Command shell with glob matching support