Filename Expansion, also known as globbing, is a feature in Linux shells that enables users to use wildcards to match multiple filenames or directory names. It simplifies file management by allowing patterns to represent a group of files rather than specifying each file individually.
How Filename Expansion Works
When a shell encounters a command with wildcards, it scans the current directory (or the specified path) for files and directories matching the pattern. The matching filenames are substituted in place of the pattern before the command is executed.
For example:
ls *.txt
- This command lists all files in the current directory with a .txt extension.
Wildcards in Filename Expansion
1. Asterisk (*)
- Matches zero or more characters.
- Example:
ls *.log # Matches all files ending with .log
ls file* # Matches files like file, file1, file123, file_name
2. Question Mark (?)
- Matches exactly one character.
- Example:
ls file?.txt # Matches files like file1.txt, file2.txt but not file12.txt
3. Square Brackets ([])
- Matches any one character within the brackets.
- You can specify:
- A set of characters: [abc]
- A range of characters: [a-z] or [0-9]
- Negation with ! or ^: [^abc] matches anything except a, b, or c.
- Example:
ls file[123].txt # Matches file1.txt, file2.txt, file3.txt
ls file[a-d].txt # Matches filea.txt, fileb.txt, filec.txt, filed.txt
4. Curly Braces ({})
- Matches a comma-separated list of strings.
- Example:
mv file{1,2,3}.txt backup/ # Moves file1.txt, file2.txt, file3.txt to backup/
cp {file1,file2}.txt /tmp/ # Copies file1.txt and file2.txt to /tmp/
5. Tilde (~)
- Expands to the home directory of the current user.
- Example:
cd ~ # Changes directory to the user’s home
ls ~/Documents/*.pdf # Lists all PDF files in the Documents directory
Combining Wildcards
Wildcards can be combined for more advanced patterns:
ls file[0-9]*.txt # Matches files starting with ‘file’ followed by a number and ending in .txt
ls *.{jpg,png,gif} # Matches files ending with .jpg, .png, or .gif
Special Characters
1. Escaping Characters
To match a literal wildcard character (*, ?, etc.), escape it with a backslash (\) or quote the pattern:
ls file\* # Matches file* exactly
ls “file*” # Matches file* exactly
2. Hidden Files
Files beginning with a dot (.) are hidden. To include them in filename expansion, use:
ls -a * # Lists all files, including hidden files
ls .* # Lists only hidden files
Practical Examples
1. Deleting Specific Files
Delete all .log files:
rm *.log
2. Copying Files with a Pattern
Copy all .txt and .csv files to a backup directory:
cp *.{txt,csv} backup/
3. Finding Specific Files
List all image files with specific extensions:
ls *.{jpg,png,gif}
4. Archiving Files
Create a tarball of all files starting with report:
tar -cvf reports.tar report*
How Shells Process Filename Expansion
- Pattern Matching:
- The shell matches the pattern against the names of files and directories in the specified path.
- Expansion:
- The matched filenames replace the pattern in the command.
- Command Execution:
- The shell executes the command with the expanded list of filenames.
Advanced Use with shopt in Bash
The shopt command in Bash allows you to control shell behavior related to filename expansion:
- Nullglob: Ensures that a wildcard that matches no files results in an empty list instead of the pattern itself.
shopt -s nullglob
ls *.nonexistent # Produces no output
- Dotglob: Includes hidden files (those starting with .) in matches.
shopt -s dotglob
ls * # Lists all files, including hidden ones
- Extglob: Enables extended globbing for more complex patterns.
shopt -s extglob
ls !(file1.txt) # Lists all files except file1.txt
Limitations of Filename Expansion
- No Recursive Matching:
- Wildcards do not match files in subdirectories unless combined with tools like find or ** with shopt -s globstar.
shopt -s globstar
ls **/*.txt # Matches .txt files in the current directory and subdirectories
- Limited Pattern Matching:
- For more complex matching (e.g., regular expressions), use tools like find, grep, or sed.
Conclusion
Filename expansion is a powerful feature of Linux shells, allowing users to efficiently work with groups of files using wildcards. Understanding and using the different types of patterns can significantly enhance productivity and make command-line tasks easier. By mastering these tools, you can handle complex file operations with minimal effort.