File Name Expansion and Management in Bash

Introduction to File Name Expansion and Management

Welcome! In this lesson, we will focus on file name expansion and management in Bash. File name expansion allows you to match file names using patterns. For example, you might want to find all .txt files in a directory or list all files that start with the word data. This lesson serves as a foundational block for managing files, significantly easing the manipulation and organization of files in more complex scripts. Let's get started!

Wildcards and Basic Expansion

File name expansion, also known as globbing, is a feature in shell environments like Bash that allows you to match filenames using patterns with special characters called wildcards. These wildcards are symbols like *, ?, and []. These wildcards allow you to easily select files that fit certain criteria without typing each filename explicitly. This simplifies file management tasks such as listing, copying, or moving multiple files at once.

Matching Any Number of Characters: `*`

The * wildcard matches any number of characters (including none). This is useful when you need to list or act on files with similar names but different suffixes or prefixes.

For example, ls *.txt lists all files ending with .txt. The * wildcard before .txt can represent any sequence of characters (including none), so any file name of any length that ends in .txt will be listed. Let's take a look:

#!/bin/bash

# Create example files
touch file1.txt draft.txt file2.sh

ls *.txt

Output

draft.txt
file1.txt

Only the files that end with .txt are included. file2.sh is not included because it does not match the specified pattern.


Now let's use * to search for any files that begin with file. ls file* matches any name that begins with file followed by any sequence of characters.

#!/bin/bash

# Create example files
touch file1.txt draft.txt file2.sh

ls file*

Output:

file1.txt
file2.sh

Only the files that start with file are included. draft.txt does not match the pattern, so it is not included.

Matching Exact Character Counts: `?`

The ? wildcard matches exactly one character. This is useful for filtering files that have specific character lengths.

For example, ls draft?.md matches any file that starts with draft and has one character before .md

#!/bin/bash

# Create example files
touch draft1.md draftA.md draft1A.md

ls draft?.md

Output:

draft1.md
draftA.md

draft1A.md is not included in the output because it has two characters between draft and .md.


You can match multiple characters by using any number of ? wildcards. Let's examine ls draft??.md. Two ? wildcards represent exactly two characters, so any file name that starts with draft and has two characters before .md will be listed.

#!/bin/bash

# Create example files
touch draft1.md draftA.md draft1A.md

ls draft??.md

Output:

draft1A.md

draft1.md and draftA.md are not included because they only have a single character between draft and .md

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal